Disabling player movement until a button is pressed

Hello developers, hope you’re all good today

Recently, I’ve been working on a game with a title screen, and I want to disable player movement until the play button on the title screen is pressed. I’ve written previous codes to see if it’d work but to no avail

here’s my most recent attempt:

local players = game:GetService("Players")
local character = player.Character
local humanoid = character.Humanoid

if menu.Visible == true then
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
else
	humanoid.WalkSpeed = 11
	humanoid.JumpPowerr = 50
end

I put this in StarterCharacterScripts and before that I attempted something similar but in the same GUI frame, any help is appreciated

Thank you in advance

3 Likes

It seems like you didn’t define the “player” in this line of code:

local character = player.Character

As well as this is a LocalScript, if you want to change a player’s WalkSpeed and JumpPower you will need to use a RemoteEvent and change the WalkSpeed and JumpPower on the server.

humanoid.JumpPowerr = 50

There is also a spelling error in this line. Do you have your output turned on? This can really help you debug your scripts. Just go to View at the top of your screen and enable “Output.”

If you have any questions feel free to ask :smile:

2 Likes

Alright, I retyped my code and added a remote event named “MenuDisablerEvent” in ReplicatedStorage, here is what I have now

local player = game:GetService("Players")
local character = player.Character
local humanoid = character.Humanoid
local menu = game.StarterGui.MainMenu.Background

if menu.Visible == true then
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
else
	humanoid.WalkSpeed = 11
	humanoid.JumpPower = 50
end

is there anything else I’d need to do?

2 Likes

You should do it with PlayerGui.

2 Likes

Yes, there is quite a few things wrong with this script.

First of all, when you define “player” you’re actually defining the player service and not an actual player. If you want to define the player you can do:

local player = game.Players.LocalPlayer -- Keep in mind defining a player this way only works inside of a LocalScript

As well as in this line here:

local menu = game.StarterGui.MainMenu.Background

You are defining the Background straight from the StarterGui. When a player first joins your game everything that is inside of the StarterGui will get copied to what’s called the player’s “PlayerGui” the “PlayerGui” shows all guis that are on that one specific player’s screen. So, to access the player’s PlayerGui you can do:

local PlayerGui = player.PlayerGui

And then get the background from there:

local menu = PlayerGui.MainMenu.Background

I always hate to say this but you seem like you have just started learning scripting. I’d recommend you watch tutorials on YouTube about Gui’s and LocalScripts it’ll really help you in the long run.

1 Like

Something as simple as this should work.
Server:

local event = path.to.event;

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(character)
    local hum = character:FindFirstChild("Humanoid");
    hum.WalkSpeed = 0;
    hum.JumpPower = 0;
  end);
end);

event.OnServerEvent:Connect(function(player)
  local character = player.Character;
  if not character then return end;

  local hum = character:FindFirstChild("Humanoid");
  if not hum then return; end;

  hum.WalkSpeed = 16;
  hum.JumpPower = 50;
end);

Client

--Either put this in your GUI and route the path to the button, or put it in the button itself.
local event = path.to.event;

script.Parent.Activated:Connect(function(inputObj, clickCount)
  event:FireServer(); --If you already have code to hide the menu, you could simply fire the remote in the function you use to dismiss the menu
end);
1 Like

Yes, I am relatively new to scripting, I am more of an art person but I wanna learn to script… hah.
Thank you for the help though, I’ll update you on what happens

3 Likes

Sorry but I don’t understand how this would work. Mind elaborating?

1 Like

Of course! Sorry, I was out riding for a little before the sun went down.

Basically we just stop the player from moving and jumping on the server when their character is added, and then when the RemoteEvent fires from the client, the server will allow the player’s character to move and jump again.

2 Likes

just do this

local Controller require(game.Players[player]:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()

then to disable/enable:

Controller:Disable() --disables movement
Controller:Enable()
2 Likes
local Controller = require(game.Players[player]:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()

But yeah, I forget about that sometimes haha. Thank you.

4 Likes

When I do it, an error pops up in output. I’ll send screenshots here.

What I put:

What Happens in output:
image

2 Likes

instead of putting

game.Players[player]

try

game.Players[player.Name]

This because the player variable your putting into it is a instance of a player, not their name

3 Likes

ill try thanks for the fix up :slight_smile:

2 Likes

I would just Anchor the humanoidRootPart

2 Likes