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

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

Asked by 4 years ago
Edited 4 years ago
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!

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. y3_th 176 — 4y
0
i think you identified "plr" as the part, thats why it searches "team" in the part and not the player.. speedyfox66 237 — 4y
0
how? MythsList 38 — 4y
0
i can't do it myself, i don't understand what you are saying MythsList 38 — 4y
View all comments (2 more)
0
nvm i figured out MythsList 38 — 4y
0
rip. i just replied with an answer. hopefully that helps clear things up royaltoe 5144 — 4y

3 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

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)
Ad
Log in to vote
0
Answered by
Filipalla 504 Moderation Voter
4 years ago
Edited 4 years ago

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)
0
Fixed it, I was trying to write it as quick as possible to post it before you or anyone else :P Filipalla 504 — 4y
Log in to vote
0
Answered by 4 years ago
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)

Answer this question