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

DataStore not working? No error.. Kind of copied a video but changed it up.

Can anyone tell me why this DataStore is not working..

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Hello")
game.Players.PlayerAdded:Connect(function(plr)
	
	local uniquekey = "id-"..plr.userId
	local leaderstats = Instance.new("IntValue",plr)
	local savevalue = Instance.new("IntValue")
	local savevalue2 = Instance.new("IntValue")
	local savevalue3 = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	savevalue.Parent = leaderstats
	savevalue.Name = "Money"
	savevalue2.Parent = leaderstats
	savevalue2.Name = "Subscribers"
	savevalue3.Parent = leaderstats
	savevalue3.Name = "Followers"
	
	local GetSaved = DataStoreService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
		savevalue3.Value = GetSaved[3]
	else
		local NumbersForSaving = {savevalue.Value, savevalue2.Value, savevalue3.Value}
		DataStoreService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = "id-"..plr.userId
	local Savetable = {plr.leaderstats.Cash.Value, plr.leaderstats.Subscribers.Value, plr.leaderstats.Followers.Value}
	DataStoreService:SetAsync(uniquekey, Savetable)
	
	
	
end)


1 answer

✓ Accepted answer

The problem is that you are calling SetAsync in the else statement in the playeradded event. You need to realise that that method yields the script until the data is found and considering that your providing the else statement with the fact it has no data to save; it will yield it forever. Here is the fix:

--MAKE SURE IT IS A SERVERSCRIPT IN SERVERSCRIPTSERVICE
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Hello")
game.Players.PlayerAdded:Connect(function(plr)

    local uniquekey = "id-"..plr.userId
    local leaderstats = Instance.new("IntValue",plr)
    local savevalue = Instance.new("IntValue")
    local savevalue2 = Instance.new("IntValue")
    local savevalue3 = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    savevalue.Parent = leaderstats
    savevalue.Name = "Money"
    savevalue2.Parent = leaderstats
    savevalue2.Name = "Subscribers"
    savevalue3.Parent = leaderstats
    savevalue3.Name = "Followers"

    local GetSaved = DataStoreService:GetAsync(uniquekey) or {} --Checks if there is data to load
    if GetSaved then
        savevalue.Value = GetSaved[1] or 0 --If there is no data then just give it a default of 0.
        savevalue2.Value = GetSaved[2] or 0 --If there is no data then just give it a default of 0. 
        savevalue3.Value = GetSaved[3] or 0 --If there is no data then just give it a default of 0.
	--Since we have already certified any errors appearing if the data is nil, there is no need for the else statement.
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = "id-"..plr.userId
    local Savetable = {plr.leaderstats.Cash.Value, plr.leaderstats.Subscribers.Value, 				 
    plr.leaderstats.Followers.Value}
    DataStoreService:SetAsync(uniquekey, Savetable)
end)

9 comments

0

@MaximumOverdriven That dosen't work.

0

It dosen't save the data

0

yes it does

0

When the player leaves itsaves the data

0

let me explain to you, getasync loads the data and if there is no data we getan empty array which represents {}, next we load the values and if there is no value, we make 0 our default; with the or . You shouldn't be setting the async in the playeradded event

0

I KNOW WHAT THE PROBLEM IS

0

Bro, I went into developer console gave me subs etc, I see it on the leaderboard and when I rejoin its gone..

0

in the player removing event, you did plr.leaderstats.Cash.Value; when it should be plr.leaderstats.Money.Value

0

accept answer if im right :)

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