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

DataStore dont save my stats. Help pls ?

I create this script , and put it in the "ServerScript Service But when i change the Value Cash1, it wont save it. I call the Cash from a localscript in Screen Gui When i start the Server again the Cash.Value is 0 ....

so i call the file


local player = game.Players.LocalPlayer
local levelpoint = player.leaderstats.Cash1

levelpoint.Value = levelpoint.Value +5


this is the DAtastore script

local DataStoreServer = game:GetService("DataStoreService") local myDataStore = DataStoreServer:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player

local Cash1 = Instance.new("IntValue")
Cash1.Name = "Cash1"
Cash1.Parent = leaderstats

local data
local succes, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId.."Cash1")
end)

if succes then
	Cash1.Value = data
else
		print("there was an error ")
		warn(errormessage)
end

end) game.Players.PlayerRemoving:Connect(function(player) local succes , errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."Cash1",player.leaderstats.Cash1.Value)

	end)

if succes then
	print("player data succesfully saved")
	warn(errormessage)
end

end)

1 answer

Script in ServerScriptService!

local dataStoreService = game:GetService('DataStoreService')
local DS = dataStoreService:GetDataStore('Cash')

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = plr
	
	local currency = Instance.new('IntValue')
	currency.Name = 'Cash'
	currency.Value = DS:GetAsync(plr.UserId) or 0
	currency.Parent = leaderstats
	
	currency.Changed:Connect(function()
		DS:SetAsync(plr.UserId, currency.Value)
	end)
end)

If you publish game, click 'Game Settings', in 'Options' enable Studio Access to API Services (click ON)! Can autosave leaderstats :) Try in console log

game.Players."Your Username".leaderstats.Cash.Value = 5

5 comments

0

I've already done it ....

0

Good!

0

did not help

0

need a publish game! in Roblox Studio click 'Game Settings', in 'Options' enable Studio Access to API Services (click ON)! um...

0

ok, and how i can change the cash from scrip ? call the leaderstat from localscript ?

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