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

This script does not respawn the player when touched when not on team red. ?

Asked by
Vxyrion 23
6 years ago
Edited 6 years ago

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)

1 answer

Log in to vote
0
Answered by 6 years ago

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.

0
Yes, I have a question im getting a error "attemping to index local 'player' (a nil value) and I cant figure out what is causing it. Vxyrion 23 — 6y
0
Is the team color you want to switch to "Really red", the BrickColor of it? @vxyrion. If not then that may be the reason. yougottols1 420 — 6y
Ad

Answer this question