Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to find a players name in workspace using a value?

Asked by 4 years ago

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

3 answers

Log in to vote
0
Answered by
DesertusX 435 Moderation Voter
4 years ago
Edited 4 years ago

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!

0
Thanks but it says that "randomPlayer is not a valid member of Players." Anyway to fix that? Thanks! kavancraft59 11 — 4y
0
Whoops! Sorry, I edited the script and it should work now. DesertusX 435 — 4y
0
don't worry if you can't help with this, but if you can, what does "attempt to index nil with WalkSpeed" mean? Thanks! kavancraft59 11 — 4y
0
I edited the script again. I'm not sure if this would work, but just try it anyways. DesertusX 435 — 4y
View all comments (2 more)
0
It says the same thing? I will try and figure out why. Thanks for your help though! kavancraft59 11 — 4y
0
No problem! Since I helped, could you please accept my answer? DesertusX 435 — 4y
Ad
Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
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

0
Thanks, but that makes everyplayer walk faster not only the seeker. Please help! Thanks! kavancraft59 11 — 4y
1
It's because of the while true, the condition on line 3 is true for too long, you should have a way on detecting if it already ran, maybe add a debounce, or increase the wait time Leamir 3138 — 4y
Log in to vote
0
Answered by 4 years ago

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
0
Thanks, but I tried that and it made every player walk faster. I just want the seeker to walk faster. Please help if possible! Thanks! kavancraft59 11 — 4y
0
Thats because you have a while loop on and use a random player every .01 seconds so you end up making every player faster SoftlockedUnderZero 668 — 4y

Answer this question