I’ve made a character selection system that works fine but, the problem is when I die the combat I gave the selected character doesn’t respawn in the backpack with me.
How do I go about tracking my old existing character and reapplying the combat I gave it after I respawn?
Here’s the selection script:
--SERVER
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local DataStore2 = require(1936396537)
local DisplayCharacterStatsModule = require(RS:WaitForChild("DisplayCharacterStats"))
local GameInProgress = RS:WaitForChild("GameInProgress")
local SelectionOn = RS:WaitForChild("SelectionOn")
local RemotesFolder = RS:WaitForChild("Remotes")
local SelectionRemote = RemotesFolder:WaitForChild("CharacterSelection")
local function SelectCharacter(player,SelectedCharacter)
for _, character in pairs(player.Characters:GetChildren()) do
if character.Name == SelectedCharacter and character.Value == true and GameInProgress.Value == false and SelectionOn.Value == true then
print(SelectedCharacter.." combat activated!")
local CharactersFolder = SS:WaitForChild("CharacterScripts"):FindFirstChild(SelectedCharacter)
if CharactersFolder then
if player.Backpack:FindFirstChild("Combat") then
player.Backpack:FindFirstChild("Combat"):Destroy()
end
local Combat = CharactersFolder:FindFirstChild("Combat"):Clone()
Combat.Parent = player.Backpack
end
end
end
end
SelectionRemote.OnServerEvent:Connect(SelectCharacter)
I updated the post to show the full server side of the script, all the client does is detect when a player clicks a gui and sends over the name of character they clicked on.
--SERVER
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local DataStore2 = require(1936396537)
local DisplayCharacterStatsModule = require(RS:WaitForChild("DisplayCharacterStats"))
local GameInProgress = RS:WaitForChild("GameInProgress")
local SelectionOn = RS:WaitForChild("SelectionOn")
local RemotesFolder = RS:WaitForChild("Remotes")
local SelectionRemote = RemotesFolder:WaitForChild("CharacterSelection")
local savedChar
local function SelectCharacter(player,SelectedCharacter)
for _, character in pairs(player.Characters:GetChildren()) do
if character.Name == SelectedCharacter and character.Value == true and GameInProgress.Value == false and SelectionOn.Value == true then
print(SelectedCharacter.." combat activated!")
savedChar = SelectedCharacter
local CharactersFolder = SS:WaitForChild("CharacterScripts"):FindFirstChild(SelectedCharacter)
if CharactersFolder then
if player.Backpack:FindFirstChild("Combat") then
player.Backpack:FindFirstChild("Combat"):Destroy()
end
local Combat = CharactersFolder:FindFirstChild("Combat"):Clone()
Combat.Parent = player.Backpack
end
end
end
end
game.Players.PlayerAdded:Connect(function (player)
player.CharacterAdded:Connect(function (char)
if savedChar ~= nil then
SelectCharacter(player, savedChar)
end
end)
end)
SelectionRemote.OnServerEvent:Connect(SelectCharacter)