So I made this script so it saves there credits or money when the leave the game and I put the default to ten credits and nothing will work.
Here is the script I have made:
local dataStores = game:GetService("DataStoreService"):GetDataStore("CreditsDataStore")
local defaultCash = 10
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)
playersLeft = playersLeft + 1
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local credits = Instance.new("IntValue")
credits.Name = "Credits"
credits.Value = 0
credits.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
character.humanoid.WalkSpeed = 16
character.humanoid.Died:Connect(function()
--They die The script Runs
if character:FindFirstChild("GameTag")then
character.GameTag:Destroy()
end
player:LoadCharacter()
end)
end)
--Data Store Stuff EPIC WAS HERE
local player_data
pcall (function()
player_data = dataStores:GetAsync(player.UserId.."-Credits")
end)
if player_data ~= nil then
--player has save data so it will load in
credits.Value = player_data
else
-- New Player Of the Game
credits.Value = defaultCash
end
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
dataStores:SetAsync(player.UserId.."-Credits",player.leaderstats.Credits.Value)
print("Data Has Been Saved No errors")
playersLeft = playersLeft - 1
bindableEvent:Fire()
end)
end)
game:BindToClose(function()
--Will Start when shut down happens
while playersLeft > 0 do
bindableEvent.Event:Wait()
end
end)