Hello, Im experiencing an issue in my game. When a player dies and respawns (or moves to a new round), their coins, inventory, and shop UI sometimes completely fail to load and the ui shows nothing. The only way to fix it is by rejoining the game.
Im not entirely sure what causes it as I dont script but I believe it may be something in the code below, which handles data loading and sending a remote to the client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local ownershipDataStore = DataStoreService:GetDataStore("OwnershipDataStore56_TESTTT")
local OtherStuff = DataStoreService:GetDataStore("OtherStuffTEST")
local currencyName = "Coins"
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local BatonNames = {
"Blue Baton",
"Electric Baton",
"Golden Baton",
"Hammer Baton",
"Lightsaber Baton",
"Magic Baton",
"Red Baton",
"Scythe Baton",
"Robux Baton",
"White Baton",
"Blade Baton",
}
local prices = {
["Blue Baton"] = 100,
["Electric Baton"] = 200,
["Golden Baton"] = 300,
["Hammer Baton"] = 400,
["Lightsaber Baton"] = 500,
["Magic Baton"] = 600,
["Red Baton"] = 700,
["Scythe Baton"] = 800,
["Vip Baton"] = 900,
["White Baton"] = 1000,
["Blade Baton"] = 1100,
}
local toolsFolder = game:GetService("ServerStorage"):FindFirstChild("Baton")
local tools = {}
for _, batonName in ipairs(BatonNames) do
tools[batonName] = toolsFolder and toolsFolder:FindFirstChild(batonName)
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 10000 -- Default if no data
coins.Parent = leaderstats
local folder = Instance.new("Folder")
folder.Name = "Ownership"
folder.Parent = player
local BatonSelected = Instance.new("StringValue")
BatonSelected.Parent = player
BatonSelected.Name = "BatonSelected"
BatonSelected.Value = "Blue Baton"
for _, batonName in ipairs(BatonNames) do
local batonValue = Instance.new("BoolValue")
batonValue.Name = batonName
batonValue.Parent = folder
end
-- Load data
local d
local success, data = pcall(function()
d = OtherStuff:GetAsync(player.UserId)
return ownershipDataStore:GetAsync(player.UserId)
end)
if success and data then
for _, batonName in ipairs(BatonNames) do
local batonValue = folder:FindFirstChild(batonName)
if batonValue then
batonValue.Value = data[batonName] or false
end
end
BatonSelected.Value = d and d["Selected"] or "Blue Baton"
else
for _, batonName in ipairs(BatonNames) do
local batonValue = folder:FindFirstChild(batonName)
if batonValue then
batonValue.Value = false
end
end
if not success then
warn("Failed to load data for " .. player.Name)
end
end
--// Load coin data
local coinSuccess, coinData = pcall(function()
return OtherStuff:GetAsync("Coins_" .. player.UserId)
end)
if coinSuccess and coinData then
coins.Value = coinData
else
warn("Failed to load coins for " .. player.Name)
end
-- VIP bonus (500 coins every 30 days)
local ownsVIP = false
pcall(function()
ownsVIP = MarketplaceService:UserOwnsGamePassAsync(player.UserId, 920470110)
end)
if ownsVIP then
local now = os.time()
local successVIP, lastBonusTime = pcall(function()
return OtherStuff:GetAsync("VIP_" .. player.UserId)
end)
if not lastBonusTime or now - lastBonusTime >= 2592000 then
coins.Value += 500
pcall(function()
OtherStuff:SetAsync("VIP_" .. player.UserId, now)
end)
end
-- Grant Golden Baton if VIP
local goldenBaton = folder:FindFirstChild("Golden Baton")
if goldenBaton then
goldenBaton.Value = true
player.BatonSelected.Value = "Golden Baton"
local tool = tools["Golden Baton"]
if tool then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end
end
task.wait(5)
Remotes.ShopReady:FireClient(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
local folder = player:FindFirstChild("Ownership")
if folder then
local data = {}
for _, batonName in ipairs(BatonNames) do
local batonValue = folder:FindFirstChild(batonName)
if batonValue then
data[batonName] = batonValue.Value
end
end
local S = {
["Selected"] = player.BatonSelected.Value
}
local success, err = pcall(function()
OtherStuff:SetAsync(player.UserId, S)
ownershipDataStore:SetAsync(player.UserId, data)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. err)
end
end
--// Save coins
local coinSuccess, coinErr = pcall(function()
local coins = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Coins")
if coins then
OtherStuff:SetAsync("Coins_" .. player.UserId, coins.Value)
end
end)
if not coinSuccess then
warn("Failed to save coins for " .. player.Name .. ": " .. coinErr)
end
end)
game.ReplicatedStorage.PurchaseEquip.OnServerEvent:Connect(function(player, batonName)
local playerCoins = player:FindFirstChild("leaderstats"):FindFirstChild("Coins")
local ownershipBaton = player:FindFirstChild("Ownership"):FindFirstChild(batonName)
if ownershipBaton and ownershipBaton.Value then
local char = player.Character
local Hum: Humanoid = char.Humanoid
Hum:UnequipTools()
for _, v in player.Backpack:GetChildren() do
v:Destroy()
end
local tool = tools[batonName]
if tool then
player.BatonSelected.Value = batonName
end
else
if prices[batonName] then
if playerCoins and playerCoins.Value >= prices[batonName] then
playerCoins.Value -= prices[batonName]
task.wait(0.5)
local char = player.Character
local Hum: Humanoid = char.Humanoid
Hum:UnequipTools()
for _, v in player.Backpack:GetChildren() do
v:Destroy()
end
local tool = tools[batonName]
if tool then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
ownershipBaton.Value = true
end
end
end
end
end)
game.ReplicatedStorage.Equip.OnServerEvent:Connect(function(ply, batonName)
local char = ply.Character
local Hum: Humanoid = char.Humanoid
Hum:UnequipTools()
for _, v in ply.Backpack:GetChildren() do
v:Destroy()
end
local tool = ReplicatedStorage.Batons:FindFirstChild(batonName)
if tool then
ply.BatonSelected.Value = batonName
else
print("Tool not found: " .. batonName)
end
end)
Any help will be appreciated thanks