Hello, I have a script that’s making the player automatically walk towards a part. I just don’t want the player to be able to move during that time, so I’m using this code to do so:
wait()
local player = game.Players.LocalPlayer
local character = player.Character
local walkable = character:FindFirstChild("Walkable")
while true do
wait()
if walkable then
if walkable.Value == true then
local Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Enable()
elseif walkable.Value == false then
local Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Disable()
end
character.Humanoid.Died:Connect(function()
local Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Enable()
end)
end
end
If they die while the controls are disabled, they won’t be able to move when they respawn. I’ve tried re-enabling it after the character is added, but it won’t work. If anyone can help, that would be a big help! Thanks in advance!
The problem will be in the Event, I think the code will run but it will do not connect to Died Event,
You should make another script where you connect this event and code it.
Sorry, I forgot to mention that I’m using a script that makes the player automatically walk to a part. I simply want to remove the ability to walk without changing the walkspeed.
I had the same problem after disableling the controls and the player died, the controls would stop working even if I used the Controls:Enable() after the player respawned, but this is how I fixed it.
Inside a local script in StartedCharacterScripts folder I added this following code:
local LocalPlayer = game:GetService("Players").LocalPlayer
local playerModule = require(LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()
controls:Disable()
wait()
controls:Enable()
I put this before any of my functions(am guessing it could go anywhere as long as it gets excecuted at the beggening)
side note just know that any scripts inside StartedCharacterScripts will reset every time the player respawns
local player = game.Players.LocalPlayer
local character = player.Character
local walkable = character:FindFirstChild("Walkable")
while true do
task.wait()
if walkable then
if walkable.Value == true then
local Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Enable()
elseif walkable.Value == false then
local Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Disable()
end
end
end
character.Humanoid.Died:Connect(function()
local Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Enable()
end)
local player = game.Players.LocalPlayer
local Controller = require(player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
player.CharacterAdded:Connect(function(char)
local walkable = char:FindFirstChild("Walkable")
char.Humanoid.Died:Connect(function()
Controller:Disable()
end)
Controller:Enable()
while task.wait() do
if walkable then
if walkable.Value then
Controller:Enable()
elseif not walkable.Value then
Controller:Disable()
end
end
end
end)
So that it detects the Died event before it loops.
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playerScripts = player:WaitForChild("PlayerScripts")
local playerModule = playerScripts:WaitForChild("PlayerModule")
playerModule = require(playerModule)
local controller = playerModule:GetControls()
controller:Enable()
local walkable = character:FindFirstChild("Walkable")
if walkable then
walkable.Changed:Connect(function(newVal)
if newVal then
controller:Enabled()
elseif not newVal then
controller:Disable()
end
end)
end