Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I trying to make a DataStore Script To Save Stats But Its Not Working?

Hello

I am trying to make a datastore script and i dont know why its not working?

Script;

--// Money DataStore Script
--// Made By XxLavaStoneexX

--// Var
local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveMoney")

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new("IntValue",plr)
	Stats.Name = "leaderstats"
	local Cash = Instance.new("IntValue", Stats)
	Cash.Value = 0 --// Money The Player Starts With
	Cash.Name = "Cash" --// The Money Value Name
	local Wins = Instance.new("IntValue", Stats)
	Wins.Value = 0
	Wins.Name = "Wins"
	
	--// Get Saved Data
	local key = "id-"..plr.userId
	local GetSavedMoney = DataStore:GetAsync(key)
	if GetSavedMoney then
		Cash.Value = GetSavedMoney[1]
		Wins.Value  = GetSavedMoney[2]
	else
		local SaveMoneyUGotNow = {Cash.Value}
		local SaveWins = {Wins.Value}
		DataStore:SetAsync(key, SaveMoneyUGotNow, SaveWins)
	end
end)

--// Auto Save Money When The Player Leaves The Server

game.Players.PlayerRemoving:Connect(function(plr)
	local key = "id-"..plr.userId
	local AutoSaveMoney = {plr.leaderstats.Cash.Value}
	local AutoSaveWins = {plr.leaderstats.Wins.Value}
	DataStore:SetAsync(key, AutoSaveMoney, AutoSaveWins)
end)

1 comment

0

any output?

1 answer

You have probarly not turned on "Enable Studio Access to API Services", to do that, go to your game, press the 3 circles then Configure Game, there you need to turn on that setting and you will be good to go. Else, goto Configure place then on Games, enable "Allow this place to be updated using the Save Place API in your game."

Second, the Instance.new("", Parent) is deprecated, use cash.Parent instead

This script will work.

--// Money DataStore Script
--// Made By XxLavaStoneexX

--// Var
local DataStore = game:GetService("DataStoreService")
local MoneyDS = DataStore:GetDataStore("SaveMoney")
local WinsDS = DataStore:GetDataStore("SaveWins")

game.Players.PlayerAdded:Connect(function(plr)
    local Stats = Instance.new("IntValue")
    Stats.Name = "leaderstats"
    Stats.Parent = plr

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash" 
    Cash.Parent = Stats

    local Wins = Instance.new("IntValue")
    Wins.Parent = Stats
    Wins.Name = "Wins"

   Cash.Value = MoneyDS:GetAsync(plr.UserId) or 0 --The 0 is default startup Money
   Wins.Value = WinsDS:GetAsync(plr.UserId) or 0 --The 0 is default startup Wins
 
end)

game.Players.PlayerRemoving:Connect(function(plr)
    WinsDS:SetAsync(plr.UserId , plr.leaderstats.Wins.Value)
    MoneyDS:SetAsync(plr.UserId , plr.leaderstats.Money.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.