Change the player team on touched has a problem

I wrote a script that, on touched, change the player team.

What is the issue?
When even a single player touches a part, all the players on the server change team and executes the function of the script.

Intent
I wanted a script to change the team and kill the local player when part (Parent) is touched.

SCRIPT:

game.Players.PlayerAdded:Connect(function(player)

--Variables--
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
			if game.Workspace.Playing.Value == true  then
				game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(lPlayer)
					lPlayer.Team = game.Teams.Playing
					print("changed player.team to Playing on touch")
				end)
				wait(1)
			Parent.Humanoid.Health = 0
			if player.Team == game.Teams.Playing then
				game:GetService("ReplicatedStorage").Events:
					WaitForChild("FromFolderToBP"):FireClient(player)
			end
		end
	end
end

	Brick.Touched:connect(PlayerTouched)
	end) -- player

--Variable
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
    local Parent = Part.Parent
    if game.Players:GetPlayerFromCharacter(Parent) then
	    if game.Workspace.Playing.Value == true  then
            for i,player in pairs(game.Players:GetPlayers()) do
                player.Team = game.Teams.Playing
                print("Changed "..player.Name.."'s Team to Playing")
	        
    game:GetService("ReplicatedStorage").Events:WaitForChild("FromFolderToBP"):FireClient(player)
               end
	    end
    end
end

Brick.Touched:Connect(PlayerTouched)

It moves all player again and it doesn’t kill player, sorry I did not specify the player kill

What do you mean ‘moves all player’?

The script set the Playing team to all players in the server

--Variable
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
    local Parent = Part.Parent
    if game.Players:GetPlayerFromCharacter(Parent) then
	    if game.Workspace.Playing.Value == true  then
            Parent.Humanoid.Health = 0
            for i,player in pairs(game.Players:GetPlayers()) do
    
                player.Team = game.Teams.Playing
                print("Changed "..player.Name.."'s Team to Playing")
	        
    game:GetService("ReplicatedStorage").Events:WaitForChild("FromFolderToBP"):FireClient(player)
               end
	    end
    end
end

Brick.Touched:Connect(PlayerTouched)

Error: Character is not a valid member of Model “Workspace.descreetOP”
I will try to remove Character

It works without Character But it set playing team to all players, output:

20:11:48.094 Changed Player1’s Team to Playing - Server - Script:14
20:11:48.094 Changed Player2’s Team to Playing - Server - Script:14
20:11:48.110 Changed Player1’s Team to Playing - Server - Script:14
20:11:48.110 Changed Player2’s Team to Playing - Server - Script:14
20:11:48.128 Changed Player1’s Team to Playing - Server - Script:14
20:11:48.128 Changed Player2’s Team to Playing - Server - Script:14

(so many times cause of touched event)

local function main(other_part)

    -- Fetch player
    local PLYRS = game:GetService("Players")
    local plyr = PLYRS:GetPlayerFromCharacter(other_part.Parent)
    local char = other_part.Parent

    -- Guard clause: if player doesn't exist, break
    if not plyr then return end

    -- Check if player is playing
    if game.Workspace.Playing.Value == true then

        -- Fetch & assign team
        local TS = game:GetService("Teams")
        plyr.Team = TS.Playing
        print(plyr.Name, plyr.Team)
        
        -- Handle events
        local RS = game:GetService("ReplicatedStorage")
        local event = RS:WaitForChild("Events"):WaitForChild("FromFolderToBP")
        event:FireClient(plyr)

        -- Handle killing the player
        char:FindFirstChildOfClass("Humanoid"):TakeDamage(100)
    end
end

script.Parent.Touched:Connect(main)
2 Likes

You have to put them yourself :smiley:

For a player kill in this script ?

Just added that. Check my previous post again.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.