Temporary freeze player when they press arrow button

Howdy :cowboy_hat_face:

I’ve created a script that changes the UI when a player presses a button (up and down). This works perfectly fine and this is how I want it. But, I have a problem with this. The player can also move with the arrow up and down keys and what I am trying to achieve is when the UI is open, and when the player presses the arrow up or down key that they don’t move but just stay in their position.

I’ve already tried to freeze the player but with no success, the player can still move. See my code here.

LocalScript

-- // SERVICES \\ --
local UserInputService = game:GetService("UserInputService")
local playerService = game:GetService("Players")
-- // VARIABLES \\ --
local localPlayer = playerService.LocalPlayer
local directionEvent = script.Parent.evs.direction
-- // MAIN SCRIPT \\ --
UserInputService.InputBegan:Connect(function(key, gpe)
	if gpe then return end
	if key.KeyCode == Enum.KeyCode.Up or key.KeyCode == Enum.KeyCode.Down then
		directionEvent:FireServer(key.KeyCode)
	end
end)

Script

local function moveButton(localPlayer, button)
	if player ~= localPlayer then player:Kick("You are firing events for other players!") return end
	-- This is where I tried to freeze the player, but with no success
end

-- // EVENTS \\ --
directionEvent.OnServerEvent:Connect(moveButton)

Sincerly, ConstructedDamage

Instead of firing the server to freeze the player, why don’t you just freeze the player on the client’s end? You can freeze them by Anchoring the HumanoidRootPart

2 Likes

Hey, I solved it by using it on the client side and using InputEnded to unfreeze the player. Thanks!

-- // SERVICES \\ --
local UserInputService = game:GetService("UserInputService")
local playerService = game:GetService("Players")
-- // VARIABLES \\ --
local localPlayer = playerService.LocalPlayer
local directionEvent = script.Parent.evs.direction
-- // MAIN SCRIPT \\ --
UserInputService.InputBegan:Connect(function(key, gpe)
	if gpe then return end
	if key.KeyCode == Enum.KeyCode.Up or key.KeyCode == Enum.KeyCode.Down then
		if script.Parent.Parent.Enabled and script.Parent.Parent.MainFrame.Visible then
			localPlayer.Character.HumanoidRootPart.Anchored = true
			directionEvent:FireServer(key.KeyCode)
		end
	end
end)

UserInputService.InputEnded:Connect(function(key, gpe)
	if gpe then return end
	if key.KeyCode == Enum.KeyCode.Up or key.KeyCode == Enum.KeyCode.Down then
		localPlayer.Character.HumanoidRootPart.Anchored = false
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.