I need to save and load peoples guns. But it doesn’t load
script:
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HTTPService = game:GetService("HttpService")
local Players = game:GetService("Players")
local playerData = DataStoreService:GetDataStore("PlayerData_1")
local Tools = ServerStorage.Tools
local Events = ReplicatedStorage.Events
local Functions = ReplicatedStorage.Functions
local function SetupGuns()
for _, gun in pairs(Tools:GetChildren()) do
local serverScript = script.Server:Clone()
local clientScript = script.Client:Clone()
serverScript.Parent = gun.Handle
clientScript.Parent = gun.Handle
serverScript.Enabled = true
clientScript.Enabled = true
end
end
local function savePlayerData(player)
local key = "Player_" .. player.UserId
local dataToSave = {
Cash = player.Cash.Value or 1000,
Guns = HTTPService:JSONEncode(player.Guns:GetChildren() or {})
}
print("Saving Cash: " .. dataToSave.Cash)
print("Saving Guns: " .. tostring(HTTPService:JSONDecode(dataToSave.Guns)))
local success, errorMessage = pcall(function()
playerData:SetAsync(key, dataToSave)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. errorMessage)
end
end
local function loadPlayerData(player)
local key = "Player_" .. player.UserId
local success, data = pcall(function()
return playerData:GetAsync(key)
end)
if success and data then
if player:FindFirstChild("Cash") then
player.Cash.Value = data.Cash
end
if player:FindFirstChild("Guns") then
print("Has guns")
print("Guns: " .. data.Guns)
for _, gunData in pairs(HTTPService:JSONDecode(data.Guns)) do
print(gunData)
local gun = Instance.new("StringValue")
gun.Name = gunData.Name
gun.Parent = player.Guns
end
end
else
warn("Failed to load data for " .. player.Name)
end
end
local function purchaseGun(player, gunName)
local tool = Tools:FindFirstChild(gunName)
if tool then
local cash = player:FindFirstChild("Cash")
if cash and cash.Value >= tool.Price.Value then
cash.Value = cash.Value - tool.Price.Value
local gunClone = tool:Clone()
gunClone.Parent = player.Backpack
local gunsFolder = player:FindFirstChild("Guns")
local gunData = Instance.new("StringValue")
gunData.Name = gunName
gunData.Parent = gunsFolder
return true
else
return false
end
else
warn("Gun not found: " .. gunName)
return false
end
end
Functions.PurchaseGun.OnServerInvoke = function(player, gunName)
return purchaseGun(player, gunName)
end
Players.PlayerAdded:Connect(function(player)
local cash = player:FindFirstChild("Cash")
if not cash then
cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 1000
cash.Parent = player
end
local gunsFolder = player:FindFirstChild("Guns")
if not gunsFolder then
gunsFolder = Instance.new("Folder")
gunsFolder.Name = "Guns"
gunsFolder.Parent = player
end
player.CharacterAdded:Connect(function(character)
loadPlayerData(player)
end)
end)
SetupGuns()
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
savePlayerData(player)
end
end)