Hi I have a character shop script that players buy characters from.
My issue is that when a player dies or resets, they will get their normal character not the equipped one that they bought from the shop.
part of the script:
local DataStoreService = game:GetService("DataStoreService")
local CharacterDataStore = DataStoreService:GetDataStore("CharacterData")
local chars = game.ReplicatedStorage:WaitForChild("Characters")
local classNames = {"Accessory", "Shirt", "Pants", "ShirtGraphic", "BodyColors"}
local function test(player,character)
for i, descendant in pairs(player.Character:GetDescendants()) do
if table.find(classNames, descendant.ClassName) or descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
descendant:Destroy()
end
end
for i, descendant in pairs(chars[character]:GetDescendants()) do
if table.find(classNames, descendant.ClassName) then
descendant:Clone().Parent = player.Character
elseif descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
descendant:Clone().Parent = player.Character.Head
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local chars = Instance.new("Folder", player)
chars.Name = "CharacterInventory"
local EquippedCharacter = Instance.new("StringValue")
EquippedCharacter.Name = "EquippedCharacter"
EquippedCharacter.Parent = player
local data = CharacterDataStore:GetAsync(player.UserId.."-Characters")
if data then
for i, CharName in pairs(data) do
if game.ReplicatedStorage:WaitForChild("Characters"):FindFirstChild(CharName) then
local Val = Instance.new("IntValue")
Val.Name = CharName
Val.Parent = chars
end
end
else
print("No Data")
end
local equippedData = CharacterDataStore:GetAsync(player.UserId.."-EquippedCharacter")
if equippedData ~= nil then
EquippedCharacter.Value = equippedData
else
print("nil")
end
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
test(player,EquippedCharacter.Value)
end)
end)
end)
When a player buys and equips from the shop it works perfectly, until the player dies or resets.
What did I do to try and solve?
I added these lines here, but it didn’t work.
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
test(player,EquippedCharacter.Value) -- ignore my function being called test
end)
end)
I think you have to wait for it to spawn a new character. You can’t set the values when the character is already dead, you have to wait for it to respawn first. You can add a
Try yielding the part of the test method that gives the objects for a little while until the player’s character is renewed(respawned), so the script doesn’t give it to the wrong character (The old character will be destoryed)
local PlayerService = game:GetService("Players")
local function test(player,character)
for i, descendant in pairs(player.Character:GetDescendants()) do
if table.find(classNames, descendant.ClassName) or descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
descendant:Destroy()
end
end
task.wait(PlayerService.RespawnTime/2) -- Yields half of the players's respawn time
local Character = player.Character or player.CharacterAdded:Wait() -- [[Pre Defined Character Variable]] Yields untill character has loaded
for i, descendant in pairs(chars[character]:GetDescendants()) do
if table.find(classNames, descendant.ClassName) then
descendant:Clone().Parent = Character
elseif descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
descendant:Clone().Parent = Character.Head
end
end
end