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:
01 | while true do |
02 | local player = game:GetService( "Players" ) |
03 | if workspace.TimeDifference.Value = = 2 and script.SeekerPicked.Value = = 0 then |
04 | local randomPlayer = game.Players:GetPlayers() |
05 | [ math.random( 1 ,#game.Players:GetPlayers()) ] |
06 | script.SeekerName.Value = randomPlayer.Name |
07 | print (script.SeekerName.Value) |
08 | workspace.(script.SeekerName.Value).Humanoid.WalkSpeed = 30 |
09 | end |
10 | wait( 0.01 ) |
11 | end |
Here is the fixed version:
1 | while true do |
2 | local player = game:GetService( "Players" ) |
3 | if workspace.TimeDifference.Value = = 2 and script.SeekerPicked.Value = = 0 then |
4 | local randomPlayer = game.Players:GetPlayers() [ math.random(#game.Players:GetPlayers()) ] |
5 | print (randomPlayer.Name) |
6 | local humanoid = randomPlayer:FindFirstChild( "Humanoid" ) |
7 | humanoid.WalkSpeed = 30 |
8 | end |
9 | end |
Don't forget to accept the answer if I helped!
01 | while true do |
02 | local player = game:GetService( "Players" ) |
03 | if workspace.TimeDifference.Value = = 2 and script.SeekerPicked.Value = = 0 then |
04 | local randomPlayer = game.Players:GetPlayers() |
05 | [ math.random( 1 ,#game.Players:GetPlayers()) ] |
06 | script.SeekerName.Value = randomPlayer.Name |
07 | print (script.SeekerName.Value) |
08 | workspace [ script.SeekerName.Value ] .Humanoid.WalkSpeed = 30 |
09 | end |
10 | wait( 0.01 ) |
11 | end |
in your script, you should index with square brackets instead of parenthesis.
Sample:
01 | local a = Instance.new( 'Part' ,workspace); |
02 | a.Name = 'Lunchable' ; |
03 |
04 | local b = workspace.Lunchable -- the period is for syntactic sugar |
05 |
06 | local c = workspace [ 'Lunchable' ] -- with square brackets (note that i used a string to index Lunchable); |
07 |
08 | local d = workspace [ a.Name ] -- the name property uses a string |
09 |
10 | print (b = = c) -- > true |
11 | print (c = = d) -- > true |
12 |
13 | -- since variables b and c are the same and variables c and d are the same |
14 | -- that means variables b and d are the same too |