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

Why my Region3 doesn't detect players properly?

Asked by
Roox4 21
3 years ago

Hey everyone.

So I'm trying to make a region where if a player goes into that region, he gets a tool, and when the player leaves the region, the tool gets removed from the player. That's it.

Now, for some reason, this only properly works when only ONE player is in the region. As soon as another player comes to the region, everything breaks.

Now, what I mean by "everything breaks" is for example that tool will not be removed from a player when he leaves the region, the tool will be given to a another player in the region or the tool won't be given to a player at all.

I've also rewritten the code multiple times and tried different methods for example with remote events but the outcome was always the same. Simply put it never worked with more than ONE player inside the region.

I have attached some screenshots and gifs below to makes this more clear.

Note: if for some reason links won't work when you click on them, copy them and paste them in your search.

This is the region: https://imgur.com/a/7KD5qAd

Here is a video when only one player is in the region: https://imgur.com/a/m8NBjMF

As you can see works just fine.

Here are videos when there are two player in the region:

Player1 POV: https://imgur.com/a/BnIWB0A

Player2 POV: https://imgur.com/a/2UyS8xm

And as you can see here it clearly doesn't work.

And here is the script:

local playersList = {}
local sword = game.ReplicatedStorage:WaitForChild("Sword")
local regionPart = workspace.RegionPart

local pos1 = regionPart.Position - (regionPart.Size / 2)
local pos2 = regionPart.Position + (regionPart.Size / 2)
local region = Region3.new(pos1, pos2)

while wait() do
    local partsInRegion = workspace:FindPartsInRegion3(region, regionPart, 1000)
    for i,v in pairs(partsInRegion) do
        if v.Parent:FindFirstChild("Humanoid") then
            player = game.Players:FindFirstChild(v.Parent.Name)
            table.insert(playersList, player)

            char = v.Parent
            hum = char:FindFirstChild("Humanoid")
            backpack = player:WaitForChild("Backpack")
            findInBackpack = backpack:FindFirstChild("Sword")
            findInCharacter = char:FindFirstChild("Sword")

            if not findInBackpack and not findInCharacter then
                if sword then
                    swordCopy = sword:Clone()
                    swordCopy.Parent = backpack
                    hum:EquipTool(swordCopy)
                end
            end
        end
    end

    for i,v in pairs(workspace:GetChildren()) do
        if v:IsA("Model") then
            if v:FindFirstChild("Humanoid") then
                local char = v
                local player = game.Players:FindFirstChild(v.Name)

                hum = char:FindFirstChild("Humanoid")
                backpack = player:WaitForChild("Backpack")
                findInBackpack = backpack:FindFirstChild("Sword")
                findInCharacter = char:FindFirstChild("Sword")

                if table.find(playersList, player) == nil then
                    if findInBackpack or findInCharacter then
                        swordCopy.Parent = nil
                        swordCopy:Destroy()
                        hum.WalkSpeed = 16
                        hum.JumpPower = 50
                    end
                end
            end
        end
    end
    playersList = {}
end

I'd be really glad if someone knew what's going on. Any help will be appreciated.

Thanks.

0
Contact my Discord. Ziffixture 6913 — 3y
0
It says invite invalid Roox4 21 — 3y

Answer this question