Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Function in local script is executed for all players?

I've created a part that when it gets touched it returns the player to a spawnpoint but when a player touches it every player in the game gets teleported even though its a local script! How is this possible?

local Teams = game:GetService("Teams")
local barrier = game.Workspace.Barrier
local player = game.Players.LocalPlayer
 
barrier.Touched:connect(function()
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")
local humanoidroot = char:WaitForChild("HumanoidRootPart")
	if player.Team.Name == "Stage1" and humanoid.Health ~= 0 then	
		humanoidroot.Anchored = true
		char:MoveTo(game.Workspace.Spawns.Checkpoint1.Position)
		wait()
		humanoidroot.Anchored = false
	end
end)

By the way, if I put the character variable outside the function it doesn't work after the player resets because it using the old character which I don't know how to fix.

1 answer

✓ Accepted answer

Ah, I figured it out. Your script executes the function when whatever player touches it. You need to add a check.

Try this

local Teams = game:GetService("Teams")
local barrier = game.Workspace.Barrier
local player = game.Players.LocalPlayer

barrier.Touched:connect(function(hit)
    if hit.Parent.Name == player.Name then
        local char = player.Character
        local humanoid = char:WaitForChild("Humanoid")
        local humanoidroot = char:WaitForChild("HumanoidRootPart")
        if player.Team.Name == "Stage1" and humanoid.Health ~= 0 then   
            humanoidroot.Anchored = true
            char:MoveTo(game.Workspace.Spawns.Checkpoint1.Position)
            wait()
            humanoidroot.Anchored = false
        end
    end
end)

(Sorry if the formatting isn't the best)

5 comments

1

Thank you so much it actually worked! I quess it was simpler than I tought.

0

No problem! Don't forget to mark my answer as correct too :)

0

how?

0

There should be a button below or above the answer.

0

nevermind the button wasn't showing but i fixed it

This discussion is preserved as it was. New answers, comments, and votes are closed.