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

Check If Multiple Players Are In A Part?

Asked by 4 years ago

So I have a Tornado Survival game, and have a feature where if you are in the map surviving, you are gaining a leaderstat point every second. This is done with a large Part named 'gain' that covers the whole map, and the script is placed inside of it. The main problem I have is that the script has it only apply to one player at a time, so if me and my friends play, I am the only one gaining points.

So I was wondering if anybody knew how to check if multiple people are colliding with a Part, and how that script would look? Thanks!

local waittime = 1
    local debounce = true
    -- This debounce variable prevents the script from spamming points

    function OnTouched(hit)
        if hit and hit.Parent and debounce then
            -- Make sure hit is isn't nil and debounce is ready
            debounce = false
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            -- Get the player from whatever bodypart hit the brick.
            if player then
                -- Make sure a player hit the brick
                local leaderstats = player:FindFirstChild("leaderstats")
                -- Make sure they have the leaderstats Value.
                local level = leaderstats and leaderstats:FindFirstChild("Money")               
-- Make sure they have the level Value in leaderstats.
                if level then
                    level.Value = level.Value + 1
                    wait(waittime)
                end
            end
            debounce = true
        end
    end

    script.Parent.Touched:Connect(OnTouched)

2 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You have to keep track of who touches and untouches the part and run through them with a loop.

local part=--part
local touching={}
local waittime=1

local running=false
function has()
    if running==false then running=true
        while #touching>0 do
            for i,v in pairs(touching) do
        if v and v:FindFirstChild("Humanoid") then
                    local plr=game.Players:GetPlayerFromCharacter(v)
            if plr then
                plr.leaderstats.Money.Value=plr.leaderstats.Money.Value+1
            end
        else
            touching[i]=nil
        end
            end
            wait(waittime)
        end
        running=false
    end
end
part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local good=true
        for _,v in pairs(touching) do
            if v==hit.Parent then
                good=false
            end
        end
        if good then
            table.insert(touching,hit.Parent)
            has()
        end
    end
end)
part.TouchEnded:Connect(function(hit)
    for i,v in pairs(touching) do
        if v==hit.Parent then
            table.remove(touching,i)
        end
    end
end)
0
I have one problem with that, when I use that script, once 1 player dies, nobody else is gaining points. puppyfriend 0 — 4y
0
@puppyfriend edited answer DanzLua 2879 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
wait(3)
local t = 1 --Time of Match
local match = false
while wait() do
    match = true
    local allplrs = game.Players:GetChildren()
    for i = 1,#allplrs do
        allplrs[i].Character.Parent = workspace.Survivors --when the player dies he will leave the folder
    end
    t = 1
    repeat
        t = t-1
    until t <= 0
    local allsv = workspace.Survivors:GetChildren()
    local plr
    for i2 = 1,#allsv do
        if #allsv > 1 then
        plr = allsv[i2].Name..","..allsv[i2].Name
        elseif #allsv == 1 then
            plr = allsv[i2].Name
        end
    end
    print("Survivors: "..plr)
    wait(3)
        for i2 = 1,#allsv do
        allsv[i2].Parent = workspace
    end
    local match = false
end

Answer this question