I’m creating a game where I want players to kill people for cash, and then save to the DataStore.
For some reason though, it won’t save, which means there’s obviously something wrong.
Leaderstats Script With Kill For Cash
game.Players.PlayerAdded:Connect(function(player) -- Leaderstats Setup
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("NumberValue") -- The value I'm trying to record
money.Name = "Money"
money.Value = 0
money.Parent = leaderstats
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value ~= nil then
leaderstats.Money.Value = leaderstats.Money.Value = + 10
end
end)
end)
end)
DataStore Script (Separate Script)
local DataStore = game.GetService("DataStoreService"):GetDataStore
game.Players.PlayerAdded:Connect(function(player)
wait()
local plrkey = "id_"..plr.userId
local save1 = plr.leaderstats.Money
local GetSaved = DataStore:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
DataStore:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value})
end)
Both scripts are in ServerScriptServer, and I have " Studio Access to API Services " and " Allow HTTP Requests " enabled in Game Settings, but for reason when I kill someone, and earn the extra cash, it won’t save when I rejoin.
Can someone tell me what I’m doing wrong?
I apologize if this has already been posted somewhere else on the DevForum. I’m relatively new to the DevForums, so I could’ve missed a post that had the same thing as this.
local DataStore: game.GetService("DataStoreService"):GetDataStore("StoreData")
game.Players.PlayerAdded:Connect(function(player)
wait()
local plrkey = "id_"..plr.userId
local save1 = plr.leaderstats.Money
local GetSaved = DataStore:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
DataStore:SetAsync(plrkey, NumberForSaving)
local success, err = pcall(function() GetSaved=DataStore:GetAsync(plrkey) end)
if not success then warn(err)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
DataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value})
end)
local Players = game:GetService("Players")
local DataStore = game.GetService("DataStoreService"):GetDataStore("StoreData")
repeat wait()
until Players
game.Players.PlayerAdded:Connect(function(player)
local plrkey = "id_"..plr.userId
local save1 = plr.leaderstats.Money
local GetSaved = DataStore:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
DataStore:SetAsync(plrkey, NumberForSaving)
local success, err = pcall(function()
GetSaved = DataStore:GetAsync(plrkey)
end)
if not success then
warn(err)
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, err = pcall(function()
DataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value})
end)
if not success then
warn(err)
end
end)
yep, i use the model for it, but the MainDatastoreScript is the only thing you wanna use when you want a datastore script Just Place it in the ServerScriptService
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local FlagBucks = Instance.new('NumberValue', leaderstats)
FlagBucks.Name = "FlagBucks"
FlagBucks.Value = 0
local value1Data = FlagBucks
local s, e = pcall(function()
value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
end)
if s then
FlagBucks.Value = value1Data --setting data if its success
else
game:GetService("TestService"):Error(e) --if not success then we error it to the console
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.FlagBucks.Value) --setting data
end)
if not s then game:GetService("TestService"):Error(e)
end
end)
Well, I figured it out a while ago, but I forgot to mark it as solution. Thanks for your help everyone!