How can i make the player not be able to move?

So i want to make the player not be able to move whilst the GUI is opened, it works however the player can then move again if they use sprinting. There is no error and any help is wanted!
Here is my GUI script:

local camera = game.Workspace.CurrentCamera

function UpdateCamera()
	camera.CFrame = game.Workspace.MenuCamera.CFrame
end

game:GetService("RunService").RenderStepped:Connect(UpdateCamera)

local button = script.Parent.Frame.ImageLabel.PlayButton
local Gui = script.Parent.Frame

local IsOpened = workspace.MenuOpened.Value
IsOpened = true
local sound = Gui.MenuSound
local player = game.Players.LocalPlayer
local OgSpeed = 16
local NewSpeed = 0
local running = game.Workspace.IsSprinting.Value

local function DoStuff()
	player.Character:WaitForChild("Humanoid").WalkSpeed = NewSpeed
	button.MouseButton1Click:Connect(function()
		Gui.Visible = false
		player.CameraMode = Enum.CameraMode.LockFirstPerson
		player.Character:WaitForChild("Humanoid").WalkSpeed = OgSpeed
		IsOpened = false
		sound:Stop()
		script:Destroy()
	end)
end
DoStuff()

Here is the sprint script

local mouse = game.Players.LocalPlayer:GetMouse()
local running = game.Workspace.IsSprinting.Value
local opened = workspace.MenuOpened.Value
running = false
function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end	


mouse.KeyDown:Connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:Connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		if opened == false then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
			
			repeat wait () until running == false
			keyConnection:disconnect()
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
				wait()
			end
		elseif opened == true then return end


	end
end)

As you can see I’ve tired implementing it, however when ever i hold sprint whilst the GUI is opened it allows me to move. Both scripts are local scripts btw

1 Like

You are setting a local value of the running value. If you’d check the IsSprinting value in the Explorer, you’d see the property is set to true (or false, not sure about your system) so you should do it like this:

local running = game.Workspace.IsSprinting
running.Value = false

Hello, thanks for the reply. Sadly, this hasn’t fixed my problem. When ever i sprint it still breaks the 0 walk speed and makes the player be able to walk normally without a restriction :frowning:. Any other ideas?

In a LocalScript, you can do:

local playerControls = require(game.Players.LocalPlayer.PlayerScripts.PlayerModule:GetControls())
playerControls:Disable()

You may be able to just anchor the HumanoidRootPart?

2 Likes

Try to change the sprint script with this

local mouse = game.Players.LocalPlayer:GetMouse()
local running = game.Workspace.IsSprinting.Value
local opened = workspace.MenuOpened.Value
running = false
function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end	


mouse.KeyDown:Connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:Connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		if opened == false and game.Players.LocalPlayer.Character.Humanoid.WalkSpeed ~= 0 then
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
			
			repeat wait () until running == false
			keyConnection:disconnect()
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
				wait()
			end
		elseif opened == true then return end


	end
end)
1 Like

yeah, I found this is the best option when you have a sprint script.

Glad I could help! If it is the solution then make sure to mark it! ← My bad thought you were post creator.

1 Like

I’m not the post creator, I was just agreeing with you

This works, except the sprint doesn’t work like you don’t walk any faster you stay at normal walking speed

The way I did this in my game was by locally setting the HumanoidRootPart to anchored, then unanchoring it when they close the menu!

game.Players.LocalPlayer.Character.HumanoidRootPart.Anchored = true
3 Likes