Still have questions? Join our Discord server and get real time help.
0

Can you change player speed to 0 using for i,v in pairs()?

hi, im kinda struggling on this script like when i use for i,v in pairs on the humanoid.WalkSpeed, the speed wont change to 0, idk why am i do it wrong or should i not using for i,v in pairs? heres the script

for i,v in pairs(game.Players:GetPlayers()) do
local char = v.Character
local hum = char:FindFirstChild("Humanoid")
hum.WalkSpeed = 0
wait(3)
hum.WalkSpeed = 16
end


btw wat i want to achive is, when the match isnt starting then all players in the match cant move or disable movement, but when the match is starting all players can walk normaly, any ideas?

0
Edited 5 months ago

Okay so i have a script that gets all the players and checks if there is an object in Workspace that also has their name, if yes then the walkSpeed of the player goes to 0 then back to 16.I tested in studio of course and it worked so it should work for you too.Also when i ran your code in studio I got an error saying attempt to index nil with find first child, I still dont know why you cant just say v.Character but oh well.

Oh yeh and this is the code:

--Script(Server)
local tab=game.Players:GetChildren()
for i,p in pairs(tab) do
local gtab=game.Workspace:GetChildren()
for i,v in pairs(gtab) do
if p.Name==v.Name then
print(v.Name)
v.Humanoid.WalkSpeed=0
wait(3)
v.Humanoid.WalkSpeed=16
end
end
end

0
sorry dude, still not working tho idk why, but when i run the script there is no printed name, Friskyman321 106 — 5mo
0
that is strange, but i think you might just need to add a wait before the script starts or try put it in a while loop if you copied in a server script because services and scripts load first sometimes they run before the objects they are referencing even exist, yumaking 63 — 5mo
0
Still doesnt workdude idk why, i tried adding wait and CharacterAdded:Wait() but still it doenst work, Friskyman321 106 — 5mo
0
yes so I tried it again and it didn't work, but it worked when I added a wait(5) so I think you should try that, I think the reason it worked for me the first time is because I had it in a while loop but for practicality in your game you shouldn't have it in a while loop of course yumaking 63 — 5mo
0
oh rlly? dang thats got to be something else man it worked for me, is there anythign else that could be the problem? like maybe you have the script in replicated storage or server storage, are there any other scripts that could affect it? could you give a screen shot of the explorer in your studio? yumaking 63 — 5mo
0
for i,v in pairs(game.Players:GetPlayers()) do