You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I would like the characterAdded function to fire on the first spawn as well. -
What is the issue? Include screenshots / videos if possible!
Hats are only given after the player dies once -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried looking on the hub but the problem seems very niche and haven’t found a specific solution
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- Script: MoneyHandler
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("PlayerMoney")
local inventoryDataStore = DataStoreService:GetDataStore("PlayerInventory")
local hatsDataStore = DataStoreService:GetDataStore("PlayerHats")
local ShopRE = game.ReplicatedStorage.ShopRE
local BuyRE = ShopRE.BuyRE
local InventoryRE = game.ReplicatedStorage.InventoryAddRE
local function NewCharacterAdded(player)
local character = player.Character or player.CharacterAdded:Wait()
local hatFolder = player:FindFirstChild("HatFolder")
if hatFolder then
for _, hat in pairs(hatFolder:GetChildren()) do
local hatAsset = game:GetService("InsertService"):LoadAsset(hat.Name):FindFirstChildOfClass("Accessory")
if hatAsset then
hatAsset.Parent = character.Humanoid
else
warn("Failed to load hat asset: " .. hat.Name)
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Zombux"
money.Value = 0 -- Start at 0 every round
money.Parent = leaderstats
local inventoryFolder = Instance.new("Folder")
inventoryFolder.Name = "Inventory"
inventoryFolder.Parent = player
local hatFolder = Instance.new("Folder")
hatFolder.Name = "HatFolder"
hatFolder.Parent = player
local success, savedMoney = pcall(function()
return moneyDataStore:GetAsync(player.UserId)
end)
if success and savedMoney then
money.Value = savedMoney
else
money.Value = 0
end
local successInventory, savedInventory = pcall(function()
return inventoryDataStore:GetAsync(player.UserId)
end)
if successInventory and savedInventory then
for _, item in pairs(savedInventory) do
local itemValue = Instance.new("StringValue")
itemValue.Name = item.id
itemValue.Value = item.name
itemValue.Parent = inventoryFolder
end
end
local successHats, savedHats = pcall(function()
return hatsDataStore:GetAsync(player.UserId)
end)
if successHats and savedHats then
for _, item in pairs(savedHats) do
local itemValue = Instance.new("StringValue")
itemValue.Name = item.id
itemValue.Value = item.name
itemValue.Parent = hatFolder
end
end
player.CharacterAdded:Connect(function()
NewCharacterAdded(player)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local money = player.leaderstats and player.leaderstats:FindFirstChild("Zombux")
if money then
local success, err = pcall(function()
moneyDataStore:SetAsync(player.UserId, money.Value)
end)
if not success then
warn("Failed to save money for player:", err)
end
end
local inventoryFolder = player:FindFirstChild("Inventory")
if inventoryFolder then
local inventoryData = {}
for _, item in pairs(inventoryFolder:GetChildren()) do
table.insert(inventoryData, {id = item.Name, name = item.Value})
end
local successInventory, errorInventory = pcall(function()
inventoryDataStore:SetAsync(player.UserId, inventoryData)
end)
if not successInventory then
warn("Failed to save inventory for player:", errorInventory)
end
end
local hatFolder = player:FindFirstChild("HatFolder")
if hatFolder then
local hatsData = {}
for _, item in pairs(hatFolder:GetChildren()) do
table.insert(hatsData, {id = item.Name, name = item.Value})
end
local successHats, errorHats = pcall(function()
hatsDataStore:SetAsync(player.UserId, hatsData)
end)
if not successHats then
warn("Failed to save hats for player:", errorHats)
end
end
end)
BuyRE.OnServerEvent:Connect(function(player, itemId, itemName, itemCost, itemRarity)
local playerMoney = player.leaderstats:FindFirstChild("Zombux")
local inventoryFolder = player:FindFirstChild("Inventory")
if playerMoney and inventoryFolder then
if itemCost <= playerMoney.Value then
if not inventoryFolder:FindFirstChild(itemId) then
local newItem = Instance.new("StringValue")
newItem.Name = itemId
newItem.Value = itemName
newItem.Parent = inventoryFolder
print("Purchase successful, " .. itemName .. " added to inventory")
playerMoney.Value = playerMoney.Value - itemCost
else
print("Already owned!")
end
else
print("Broke")
end
end
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.