So, I have a serverscript that only checks when the player joins the game; The only problem is that I want it to check when the player changes their team and resets. I will provide the code below, how would I do this?
Code
local TeamService = game:GetService("Teams")
local GroupID = 10158175
local Player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
if player.Team == TeamService["KGB"] and player:IsInGroup(GroupID) then
ReplicatedStorage.Tools.Test = Player.Character
else
print("Player is not in group and/or is not on the KGB team")
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.Character:WaitForChild('Humanoid').Died:Connect(function()
-- Do stuff here for the player
end)
end)
That means the character hadn’t loaded yet. Try this:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild('Humanoid').Died:Connect(function()
-- Do stuff here
end)
end)
end)
--Script in ServerScriptService
local teams = game:GetService("Teams")
local rep = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(plr)
print("player added")
plr.CharacterAdded:Connect(function(char)
print("character added")
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
print("died")
if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
print("validated, cloning..")
rep.Tools.Test:Clone().Parent = plr.Backpack
end
end)
end)
end)
I think the reason it isn’t working is because the system I use for resetting is refreshing. Instead of resetting the character, it quickly just refreshes them.
local function loadCharacter(client)
if client then
if client.Character ~= nil then
else
client:LoadCharacter()
end
end
end
CharacterLoader.OnServerEvent:Connect(loadCharacter)
You should make it easier by using a
StarterCharacter Script saying
local TeamService = game:GetService("Teams")
local hum= script.Parent.Humanoid
local GroupID = 10158175
local Player = game.Players:GetPlayerFromCharacter(script.Parent)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
hum.Died:Connect(function()
if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
print("validated, cloning..")
rep.Tools.Test:Clone().Parent = plr.Backpack
end
end)
local function loadCharacter(client)
if client then
if client.Character ~= nil then
else
client:LoadCharacter()
if plr.Team == teams["KGB"] and plr:IsInGroup(10158175) then
print("validated, cloning..")
rep.Tools.Test:Clone().Parent = plr.Backpack
end
end
end
end
CharacterLoader.OnServerEvent:Connect(loadCharacter)