Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

6 comments

0

Thanks but it says that "randomPlayer is not a valid member of Players." Anyway to fix that? Thanks!

0

Whoops! Sorry, I edited the script and it should work now.

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!

0

I edited the script again. I'm not sure if this would work, but just try it anyways.

0

It says the same thing? I will try and figure out why. Thanks for your help though!

0

No problem! Since I helped, could you please accept my answer?

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

2 comments

0

Thanks, but that makes everyplayer walk faster not only the seeker. Please help! Thanks!

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

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

2 comments

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!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.