Restricting player movement doesn't work

Hey, so I am basically trying to restrict player movement (so a player cannot move) and I made this script (which works 99% of the time) but it isn’t fully safe. Once in a while it doesn’t work, but I don’t get any errors either.

Don’t what it could be. Thanks for checking it out :slight_smile:

My script:
A local script inside the StarterPlayerScripts:

--//Player and character
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait()

--//Services
local UIS = game:GetService("UserInputService")
local guiService = game:GetService("GuiService")
local tweenService = game:GetService("TweenService")

--//Disconnect regular character movement
local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()
warn("Controls")
controls:Disable()
warn("Controls disabled") --This prints always (Even the times when the controls were not disabled)

You might be disabling the controls before they’re properly initialized? Try waiting a little bit before doing that.

Do you need the controls at all cuz if you don’t need them can you not just destryoy the UI?

local character = player.Character or player.CharacterAdded:Wait()

Yeah, I was guessing it must be something like that.

I must mention that I use playerFolder.CharacterAutoLoads = false in another local script. This might be it?

(The player spawns a few seconds after)

I’m not entirely sure, I personally use a similar method to disable controls, never had this issue before though. I don’t instantly disable controls as you do though. Try waiting a few seconds just for testing purposes and see if it fixes itself.

In addition to my previous post you may want to wait for the local player’s “PlayerScripts” folder to replicate.

Thanks! I think I might try to disable the controls in the same script where I spawn them.

That’s a good idea. But I thought it wasn’t necessary as I didn’t get any errors. But I’ll add it - just in case. Thanks for the suggestion.

This too.

If this script is located inside StarterCharacterScripts it won’t be cloned into the player’s character until it has been added/loaded. This means that “.CharacterAdded” may have already fired by the time the script executes.

Honestly, I don’t think controls directly affect your character at all, just your input which in turn affects your walking/jumping, etc. Waiting for the character to spawn might be unnecessary, try removing that and see if it still works.

1 Like

He says that “Controls disabled” still prints even when it is enabled so I’m not sure that’s the case, unless he is misreading the console.

Yeah, I thought so too.
I’m pretty sure (and it should) that the control stuff doesn’t need to wait for the character. But, who knows.

local players = game:GetService("Players")
local player = players.LocalPlayer
local playerScripts = player:WaitForChild("PlayerScripts")
local playerModule = playerScripts:WaitForChild("PlayerModule")
playerModule = require(playerModule)
local playerControls = playerModule:GetControls()
playerControls:Disable()

This script is working on my end StarterPlayerScripts or StarterCharacterScripts.

Thanks!
The thing is just that it works most of the time. But like 1 in 20 times it doesn’t which is weird.

@Synteuro @Forummer

So I think I found out how it’s better. So, as we talked about @Synteuro, the players can be disabled before the character loads. So I basically took the controls:Disable() stuff before the characterAdded:Wait().

Thanks for the help! :slight_smile:

1 Like