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

Script not working? (ServerSided) ForceField Script Depending on team

Asked by
OFF_S4LE 127
3 years ago

Script inside server script service:

local Safe = game.Teams.Safe
local player = game.Players.LocalPlayer
local ForceField = Instance.new("ForceField")
if player.Team == Safe then
ForceField.Name = "NewForceField"
ForceField.Parent = player.Character
end

1 answer

Log in to vote
1
Answered by 3 years ago

You cannot access the LocalPlayer from the server, only on the client. Instead you would need to do something like this...

local Safe = game.Teams.Safe

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        if player.Team == Safe then
            local forcefield = Instance.new("ForceField")
            forcefield.Name = "NewForceField"
            forcefield.Parent = character
        end
    end)
end

Alternatively this may not work due to the team being assigned when the character already exists, so you could also do something like this...

local Safe = game.Teams.Safe

game.Players.PlayerAdded:Connect(function(player)
    -- listen to when the player's team is changed
    player:GetPropertyChangedSignal("Team"):Connect(function()
        if player.Team == Safe then
            if player.Character then
                local forcefield = Instance.new("ForceField")
                forcefield.Name = "NewForceField"
                forcefield.Parent = player.Character
            end
        end
    end)
end
0
Thanks ! The first script worked for me with a bit of editing, you forgot a parenthesis at the last end OFF_S4LE 127 — 3y
Ad

Answer this question