Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created September 14, 2022 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/0b2d5139da78735d7ac798af44584b6d to your computer and use it in GitHub Desktop.
Save Shilo/0b2d5139da78735d7ac798af44584b6d to your computer and use it in GitHub Desktop.
Click to move example in Roblox Studio.
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local effect = game.ReplicatedStorage.ClickToMoveEffect
local heartbeatSignal = nil
local function OnClickToMoveTick()
local hit = player:GetMouse().Hit
if not hit then return end
player.Character.Humanoid:MoveTo(hit.Position)
effect.Position = hit.Position
effect.Effect.Enabled = true
effect.Parent = workspace
end
local function OnClickToMove(actionName, inputState, inputObject)
if heartbeatSignal then heartbeatSignal:Disconnect() end
if inputState ~= Enum.UserInputState.Begin then
effect.Effect.Enabled = false
return
end
OnClickToMoveTick()
heartbeatSignal = RunService.Heartbeat:Connect(OnClickToMoveTick)
end
ContextActionService:BindAction("ClickToMove", OnClickToMove, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch)
local ACTION_CLICK_TO_MOVE = "ClickToMove"
local ACTION_CLICK_TO_MOVE_UPDATE = ACTION_CLICK_TO_MOVE.."Update"
local ContextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local effect = game.ReplicatedStorage.ClickToMoveEffect
local function OnClickToMove(actionName, inputState, inputObject)
if actionName == ACTION_CLICK_TO_MOVE and inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
if inputState == Enum.UserInputState.Begin then
ContextActionService:BindAction(ACTION_CLICK_TO_MOVE_UPDATE, OnClickToMove, false, Enum.UserInputType.MouseMovement)
else
ContextActionService:UnbindAction(ACTION_CLICK_TO_MOVE_UPDATE)
end
end
if inputState ~= Enum.UserInputState.Begin and inputState ~= Enum.UserInputState.Change then
effect.Effect.Enabled = false
return
end
local hit = player:GetMouse().Hit
if not hit then return end
player.Character.Humanoid:MoveTo(hit.Position)
effect.Position = hit.Position
effect.Effect.Enabled = true
effect.Parent = workspace
end
ContextActionService:BindAction(ACTION_CLICK_TO_MOVE, OnClickToMove, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment