How to stop a player from moving by pressing a key?

I made a elemental spell thing and i don’t want the player to move while pressing Z (casting it)
How can i achieve this using script?

You can use a remoteEvent to change a boolean parented to the player. This would set their speed to 0 and back.

1 Like

Even if i press A and D (left or right) it wont move to that direction too?

?
You would change ZisPressed when Z gets pressed or when it was released
while ZisPressed == true
–target player set speed to 0
end
if ZisPressed == false
– target player set speed back
end

1 Like

You don’t have to use a remote event for this? It’s better to use local script

2 Likes

Here’s a script that should stop the player from moving and rotating when you press “F”, change this in the .KeyCode conditional line:

Add a wait() after:
humanoid.WalkSpeed = 0
humanoid.AutoRotate = false
To re-enable autorotate, and set walkspeed to 16 (default speed)

-- We must get the UserInputService before we can use it
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local function onInputBegan(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		print("is keyboard")
		if input.KeyCode == Enum.KeyCode.F then	-- button to freeze player
			print("pressed e")
			local character = LocalPlayer.Character
			local humanoid = character:WaitForChild("Humanoid")
			humanoid.WalkSpeed = 0
			humanoid.AutoRotate = false
			print("changed")
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)
5 Likes