#Touched Event fires twice

1 messages · Page 1 of 1 (latest)

lime hollow
#

So When I touch a specific part, I want the script to switch me from a team to another so I made a script that creates the team and then switches the player but the script executes twice and It says You're in the 'Base' team twice

local part = script.Parent

local function AssignBaseCamp(touchPart)
    
    if touchPart.Parent:FindFirstChild("Humanoid") then
        local newTeam = Instance.new("Team",game.Teams)
        newTeam.AutoAssignable = false
        newTeam.Name = "Base"
        
        local playerInstance = game.Players:GetPlayerFromCharacter(touchPart.Parent)
        
        if playerInstance.Team ~= newTeam then
            print("Test")
            playerInstance.Team = newTeam
        end
    end
    
end

part.Touched:Connect(AssignBaseCamp)```
jaunty steeple
lime hollow
#

this thing following me everytime I use touched event

#

I'm trying to use remote function

#

I think I'm just making it harder for myself..

jaunty steeple
#

i'm not sure

lime hollow
#

You're SCRIPTER!

#

HELP ME!

#

It's impossible for you to script without fixing touched event!

jaunty steeple
#

you already tried the thing I was about to suggest

lime hollow
#

We could wait! some guy out there have the answer 😬

#

If i switched the team in local script it's only going to switch on client side?

slow atlas
#

still need help ?

lime hollow
#

Yeah

slow atlas
#

what the prob ?

slow atlas
#

you need a debounce

jaunty steeple
lime hollow
normal pumiceBOT
#

studio** You are now Level 3! **studio

slow atlas
#

local part = script.Parent

local debounce = false

local function AssignBaseCamp(touchPart)

if touchPart.Parent:FindFirstChild("Humanoid") then
    if debounce then return end
    debounce = true
    local newTeam = Instance.new("Team",game.Teams)
    newTeam.AutoAssignable = false
    newTeam.Name = "Base"

    local playerInstance = game.Players:GetPlayerFromCharacter(touchPart.Parent)

    if playerInstance.Team ~= newTeam then
        print("Test")
        playerInstance.Team = newTeam
    end
    task.wait(1)
    debounce = false
end

end

part.Touched:Connect(AssignBaseCamp)

slow atlas
#

i read fast

lime hollow
#

Okay, you seem different

#

Imma try my friend

slow atlas
#

ok

lime hollow
#

same

#

same result

#

Executes twice but the print once

#

It says I'am in the base team twice in the chat

#

but prints once on output

slow atlas
slow atlas
lime hollow
lime hollow
lime hollow
#

00:07:52.866 ▶ Test (x9) - Server - AssignBaseCamp:17

#

You didn't solve it agony

slow atlas
#

try that

#

local part = script.Parent

local debounce = false

script.Parent.Touched:Connect(function(hit)
if debounce then return end
debounce = true
if not hit.Parent:FindFirstChild("Humanoid") then return end
local newTeam = Instance.new("Team",game.Teams)
newTeam.AutoAssignable = false
newTeam.Name = "Base"
local playerInstance = game.Players:GetPlayerFromCharacter(hit.Parent)
if playerInstance.Team ~= newTeam then
print("Change Player Team")
playerInstance.Team = newTeam
end
task.wait(1)
debounce = false
end)

lime hollow
#

okay

#

same

#

same result

slow atlas
#

same error ?

lime hollow
#

yeah

slow atlas
#

do an screen ( for new error line )

#

of the output

lime hollow
#

it says once now

#

weird

slow atlas
#

no error ?

lime hollow
#

I want it to write you are in the base team once if its possible

lime hollow
slow atlas
#

so it work

#

the 2 message in chat arent accurate, use print for accurate number of how many time an script run

slow atlas
lime hollow
#

Cus Imma put it solved if its that way

slow atlas
#

if you arent really sure, let the post on not solve but as what i see the problem seam to be solve

lime hollow
#

Okay I might consider it's solved but Ima wait for a different answer other than debounce

#

but you seem right about the chat cus it prints once

lime hollow
lime hollow
# slow atlas ok

Just noticed something, if the team was already made and not in script, the message You're in base Team will show up only once but if I create the team on the script the message will show up twice.

slow atlas