Why it won't work?

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?? :tongue:
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
1 Like

Is it a local script?. . . . . .

Question, why are you changing player team from client side?
And I can’t seem to understand your code.

Please mention:

  • What are you trying to make?
  • Have you tried to find more topics similar to this on devforum?
  • Have you read documentation?

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)

A set of useful links:

Client-Server Model
Players Service
Teams Service
Team Class
Touched Event

4 Likes

Excellent explanation as opposed to my super lazy:

lol

2 Likes

Btw where should I run a script that gives a player some money when clicking on a clickdetector? Client or server?? Thanks to everyone who answered :slight_smile: