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

Function in local script is executed for all players?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
1
Answered by
Torren_Mr 334 Moderation Voter
3 years ago
Edited 3 years ago

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)

1
Thank you so much it actually worked! I quess it was simpler than I tought. iraklisonic354 13 — 3y
0
No problem! Don't forget to mark my answer as correct too :) Torren_Mr 334 — 3y
0
how? iraklisonic354 13 — 3y
0
There should be a button below or above the answer. Torren_Mr 334 — 3y
0
nevermind the button wasn't showing but i fixed it iraklisonic354 13 — 3y
Ad

Answer this question