I’m trying to make a door that (when touched) changes the player’s team. I wrote a script (it’ll be in the end) but it doesn’t work. The names are ok, I checked (I’m a newbie so they still can be wrong). Here’s the output (don’t mind the name of the door, I forgot to change it):
I don’t even know what’s wrong. Can anyone help??
The script:
local player = game:GetService("Players").LocalPlayer
local playerTeam = player.Team
local scriptTeam = game.Teams.red
local function teamChange()
playerTeam = scriptTeam
end
local function touched()
if Door.Touched then
teamChange()
end
end
From the error log, it seems that this is a ServerScript. The LocalPlayer object of the Players service only works in LocalScripts (scripts that run on the client, they’re either put in StarterPlayerScripts or StarterCharacterScripts).
This means that you’ll have to get the player who touches the door from the touched event itself, like so:
-- Defining the services we'll be using.
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
-- Creating the red team and setting its properties.
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.Name = "Red Team"
redTeam.Parent = Teams
-- Assuming door is a part, parts have an event called Touched.
-- This event fires whenever another part (the touch parameter) touches them.
Door.Touched:Connect(function(touch)
-- Getting the parent of the touching part, and checking if it exists.
-- We assume that it's a character here.
local character = touch.Parent
if character then
-- Having checked that this 'character' exists, we'll get its player.
local player = Players:GetPlayerFromCharacter(character)
-- Then we check if the player is valid, which means that the player-
-- -exists and that 'character' is, infact, a character like we assumed.
if player then
-- Lastly, we set the player's team to the red team we created.
player.Team = redTeam
end
end
end)