Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to give "plr" a specific team when touching the part where the script is in?

local part = script.Parent
local team = game.Teams["Team"]

part.Touched:Connect(function(plr)
	plr.Team = team
end)

5: Team is not a valid member of Part

How do i do that?

Thanks for helping me if you do!

6 comments

0

Hit is a part, not a player. If you want to get a player from a part, then get the part's parent and use Player:GetPlayerFromCharacter() in order to get the player from the character. You also have to check if you get a player from the part's parent or not.

0

i think you identified "plr" as the part, thats why it searches "team" in the part and not the player..

0

how?

0

i can't do it myself, i don't understand what you are saying

0

nvm i figured out

0

rip. i just replied with an answer. hopefully that helps clear things up

3 answers

✓ Accepted answer

Hit is the part that hit the touch part. The thing hitting the part may or may not be part of a character. You can check if the part touching the touch part is a player by using Players:GetPlayerFromCharacter(hit.Parent)

the code below checks if the thing touching the part is a player, if it is not, then exit out of the function:

local Players = game:GetService("Players")
local part = script.Parent
local team = game.Teams["Team"] --"Team" is your team name

part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end

	player.Team = team
end)

Touched fires with the Part it touched, not the Player so we have to get the Player from the Character. The

local Players = game:GetService("Players")
local Part = script.Parent
local Team = game.Teams["Team"]

Part.Touched:Connect(function(TouchedPart)
	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
	if Player then
		Player.Team = Team
	end
end)

1 comment

0

Fixed it, I was trying to write it as quick as possible to post it before you or anyone else :P

local part = script.Parent
local team = game.Teams["Team"]

part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("HumanoidRootPart") then    
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    player.Team = team
end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.