I was making admin commands for a certain game, with this script focusing on the walk speed. The person says "!walkspeed [player] [speed]" but nothing comes up, neither an error or a good result. Here's the script in ServerScriptService:
game.Players.PlayerAdded:Connect(function(Player) Player.Chatted:Connect(function(Message) if Player.UserId == 1446387420 then local Arguments = string.split(Message, " ") local speed = tonumber(Arguments[3]) if Arguments[1] == "!walkspeed" then local Target = Arguments[2] game.Workspace[Target].Humanoid.WalkSpeed = speed end end end) end)
P.S. I double-checked that the user ID is correct.
Any help will be... well... helpful!
use print
, good way to debug what's wrong:
game.Players.PlayerAdded:Connect(function(Player) print("Player has joined the game") Player.Chatted:Connect(function(Message) print("Message out:", Message) if Player.UserId == 2536746511 then print("User is admin") local Arguments = string.split(Message, " ") local speed = tonumber(Arguments[3]) print("Arguments:", Arguments) print("Speed:", speed) print("Command:", Arguments[1]) if Arguments[1] == "!walkspeed" then print("Command is !walkspeed") local Target = Arguments[2] print("Target name:", Target) print("Target object:", workspace:FindFirstChild(Target)) game.Workspace[Target].Humanoid.WalkSpeed = speed print("Speed of object was changed successfully") end end end) end)
Out of these prints, if you:
PlayerAdded
, to fix this, you can call the function for existing players:local function OnPlayerAdded(Player) -- previous code end for _, Player in ipairs(game.Players:GetPlayers()) do task.spawn(OnPlayerAdded, Player) end game.Players.PlayerAdded:Connect(OnPlayerAdded)
task.spawn
will make sure that the function will run independently from the loop just in case more players are in the game before PlayerAdded
fires
Don't see "User is admin" then you can be playing with a different account, but with your double ID check that won't be the case
Don't see "Command is !walkspeed" then you type the command incorrect, there can be double space in your chat message for example
In "Target:" target name is empty, this is same case as before, you could include one more space in the command, that will turn the Arguments
table to be one field bigger and will break your script
See "Target object: nil", this means that you wrote the name incorrectly, though since you said there are no errors this should not be the case too
See "Speed of object was changed successfully" then make sure the speed you typed in command is not 16 or 17 or close to these numbers since then you won't see difference between new and old speed, if you see this message but the speed does not change, there can be a different script changing the speed?