Can somebody please help me? I'm making a hide and seek game where a random player is selected as seeker and they have a higher walking speed. However, I don't know how to find that player in the workspace. I am fairly new to Roblox scripting and can someone help me out. Thanks!
here is the script:
while true do local player = game:GetService("Players") if workspace.TimeDifference.Value == 2 and script.SeekerPicked.Value == 0 then local randomPlayer = game.Players:GetPlayers() [math.random(1,#game.Players:GetPlayers())] script.SeekerName.Value = randomPlayer.Name print (script.SeekerName.Value) workspace.(script.SeekerName.Value).Humanoid.WalkSpeed = 30 end wait(0.01) end
Here is the fixed version:
while true do local player = game:GetService("Players") if workspace.TimeDifference.Value == 2 and script.SeekerPicked.Value == 0 then local randomPlayer = game.Players:GetPlayers()[math.random(#game.Players:GetPlayers())] print (randomPlayer.Name) local humanoid = randomPlayer:FindFirstChild("Humanoid") humanoid.WalkSpeed = 30 end end
Don't forget to accept the answer if I helped!
while true do local player = game:GetService("Players") if workspace.TimeDifference.Value == 2 and script.SeekerPicked.Value == 0 then local randomPlayer = game.Players:GetPlayers() [math.random(1,#game.Players:GetPlayers())] script.SeekerName.Value = randomPlayer.Name print (script.SeekerName.Value) workspace[script.SeekerName.Value].Humanoid.WalkSpeed = 30 end wait(0.01) end
in your script, you should index with square brackets instead of parenthesis.
Sample:
local a = Instance.new('Part',workspace); a.Name = 'Lunchable'; local b = workspace.Lunchable -- the period is for syntactic sugar local c = workspace['Lunchable'] -- with square brackets (note that i used a string to index Lunchable); local d = workspace[a.Name] -- the name property uses a string print(b==c) -- > true print(c==d) -- > true -- since variables b and c are the same and variables c and d are the same -- that means variables b and d are the same too