Hello, Im trying to get the player to respawn when on the other team. Can I get some help?
script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then player.Team = game.Teams.Red end if player and not game.Teams.Red:GetPlayers(hit.Parent.Name) then player:LoadCharacter() end end)
(I only want the player to get respawn and teamed when not on red)
Problem:
You have the concept of what you are trying to do correctly but you just did not type the code correctly. Which is understandable as you may be learning and the learning process is awesome yet also daunting at times. I do not know why you have lines 2-4 as it seems you are trying to assign them to that team, but you said that you just wanted them to respawn if not on that team so I will leave that alone.
Solution:
Since you have the concept correct it is pretty straightforward from here. Whenever you want to use a ServerScript
to find a player through a Touch event
to find their team
you would use the :GetPlayerFromCharacter()
method, which is what you did. The thing is you would also need to do a for loop
to achieve this result and do if statements
to check. As I did below:
Script:
local debounce = true script.Parent.Touched:Connect(function(hit) if debounce == true then -- Added a debounce to prevent spam print("debounce is turning off") debounce = false local player = game.Players:GetPlayerFromCharacter(hit.Parent) --You got this right for i,v in pairs(game.Players:GetPlayers()) do --You need to search through the players with a for loop if v.Name == player.Name then --Checking if it is the player print("Found player "..v.Name) if v.TeamColor ~= BrickColor.new("Really red") then --Checks desired team print("Not in this team") v:LoadCharacter() debounce = true else --Just so if they are already on the team to do nothing but print they are print("You are on this team") debounce = true end end end end end)
If you have any problems or you do not understand anything do not be afraid to ask. If this helped you do not forget to give an upvote and accept my answer.