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

Locate character in a minigames script?

Asked by 8 years ago

I'm trying to detect if there is an "alive" value in the player. If the value is there, the character is alive If the value isn't there, the character isn't. (not a specific player)

How would I do this?

        for roundtime = 30, 1, -1 do
            local player = 
            if player.Character:FindFirstChild("inGame") == nil then
            playersalive.Value = playersalive.Value -1
            end
            if playersalive.Value == 1 then break end
            wait(1)
            text.Value = "Game time: "..roundtime
        end

2 answers

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

You need to loop through the players and perform the actions on each of them.

for roundtime = 30, 1, -1 do
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Character and player.Character:FindFirstChild("inGame") == nil then
            playersalive.Value = playersalive.Value -1 -- I'm assuming this was defined previously
        end
    end
    if playersalive.Value <= 1 then break end -- In case it drops beneath 1 (everyone is out of the game).
    wait(1)
    text.Value = "Game time: "..roundtime
end

Hope this helped.

EDIT: Since it's taking so long, you could try having the table already present in the script so you don't have to get it each time, and see if that helps.

local players = game.Players:GetPlayers()
game.Players.PlayerAdded:connect(function(player)
    players[player] = true
end)
game.Players.PlayerRemoving:connect(function(player)
    if players[player] then
        players[player] = nil
    end
end)
for roundtime = 30, 1, -1 do
    for _, player in pairs(players) do
        if player.Character and player.Character:FindFirstChild("inGame") == nil then
            playersalive.Value = playersalive.Value -1 -- I'm assuming this was defined previously
        end
    end
    if playersalive.Value <= 1 then break end -- In case it drops beneath 1 (everyone is out of the game).
    wait(1)
    text.Value = "Game time: "..roundtime
end

If this doesn't work, I'm not sure; maybe someone else can answer better. Hope this helped.

0
Thank you! HighValue 40 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

You can look through everything in Workspace, see if it has a Humanoid, then see if the value which is in the model. It's not too complicated. Just put this bit of code where you need it.

for i,v in pairs(game.Workspace:GetChildren()) do -- Getting a table of everything in Workspace
    if v:FindFirstChild("Humanoid") then -- Check if the model has a Humanoid. (only characters have a humanoid in them unless you/a script inserts one in)
        if v:FindFirstChild("inGame") -- Assuming that's the value you're looking for.
            -- whatever you want the script to do if the value is there.
        else -- just so the script wont error
            print("player has died")
        end
    end
end

Answer this question