Function does not break

Hey,

my code:

local TeamChecker = script.Parent


TeamChecker.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") then
        print("touched works")
        local Tycoons = game.Workspace["Zednov's Tycoon Kit"].Tycoons
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        for _,child in pairs(Tycoons:GetChildren()) do
            if child.Owner.Value == nil then
                print("child found")
                player.TeamColor = child.TeamColor.Value
                child.Owner.Value = player
                player.RespawnLocation = child.Essentials.Spawn
                hit.Parent:WaitForChild("Head"):Remove()
                print("before break")
                break
            end
        end
        wait(0.1)
    end
end)

The problem is, every child.Owner.Value gets the same player as Value… The team color only works for the first one who joined.


(Only in the yellow park is someone)
It worked when i had all the tycoon spawns set to neutral enabled, I now disabled it because that are team spawns. All players spawn on the part where the script is located in. But they don’t get resetted + they don’t get a team.

Means, every Tycoon got the same owner, but the owner should only have 1 tycoon. Somehow the break doesn’t work :confused:

Isn’t that a for loop? And return doesn’t work :confused:

I thought it was a function but I just realized

The touched event is firing more than once I’m assuming.
Disconnect the touched event or check if they are already on a team.

I added a debounce, but its still not working.

local TeamChecker = script.Parent


TeamChecker.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") then
        print("touched works")
        local Tycoons = game.Workspace["Zednov's Tycoon Kit"].Tycoons
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        for _,child in pairs(Tycoons:GetChildren()) do
            if child.Owner.Value == nil and player.Team == game.Teams['For Hire'] then
                print("child found")
                player.TeamColor = child.TeamColor.Value
                child.Owner.Value = player
                player.RespawnLocation = child.Essentials.Spawn
                hit.Parent:WaitForChild("Head"):Remove()
                print("before break")
                break
            end
        end
        wait(0.1)
    end
end)
1 Like

it works!!! Thank you so much :slight_smile:

1 Like

Could I replace the Head Remove with LoadCharacter(), or would that cause issues?

You could do that or just teleport them

Would I do player:LoadCharacter() or hit.Parent:LoadCharacter()

You would use player:LoadCharacter()

1 Like