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

How would I find an object with the same name as a player?

Asked by 9 years ago

I am making something for when a player dies this boolvalue changes to true, it won't work though

function onHumanoidDied(humanoid, player)
 for _, playerdead in pairs (game.Workspace.PlayerStuffs:FindFirstChild(player.Name)) do
    playerdead.Value = true
 end
end

Thats the code.

1 answer

Log in to vote
3
Answered by
Unclear 1776 Moderation Voter
9 years ago

FindFirstChild does not return a table, it returns the first object found matching the string you give it.

Therefore, your loop will not actually run because there is no table for it to iterate through!

You should also just get rid of the humanoid parameter in your function; you never actually end up having to use it.

You should revise your function to...

function onHumanoidDied(player)
    local playerDead = workspace.PlayerStuffs:FindFirstChild(player.Name)
    if playerDead and playerDead:IsA("BoolValue") then 
        -- check if the object exists and is a boolvalue
        -- if it is, set the object's value to true
        playerDead.Value = true
    end
end

Ad

Answer this question