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

What is wrong with this DataStore script?

Hello, I wrote this script to test saving any leaderstats value and unfortunately it will not save the 'Schmeckles' value. Is anybody experienced enough with DataStore that they could point out what I've done wrong? I left the script in Server Script Storage.

local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency')

game.Players.PlayerAdded:connect(function(player)
	
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	local Schmeckles = Instance.new("IntValue", stats)
	Schmeckles.Name = "Schmeckles"
	
	local key = "player-"..player.userId
	
	local SavedValues = DataStore:GetAsync(key)
	
	if SavedValues then
		Schmeckles.Value = SavedValues[1]
	else
		DataStore:SetAsync(key, Schmeckles.Value)
	end
end)

game:BindToClose(function()
	
	game.Players.PlayerRemoving:connect(function(player)
	
	local key = "player-"..player.userId
	
	local Schmeckles = player.leaderstats.Schmeckles.Value
	DataStore:SetAsync(key, Schmeckles)
	

	end)
end)

5 comments

0

try removing .Value from line 27 and adding it to Schmeckles on line 28

1

Why are you connecting the PlayerRemoving event when the server is shutting down? You are also saving the value but accessing it trying to load it as a table.

0

Provide us with the error.

0

It's all I could grasp from the wiki and numerous videos, thankyou for the comments I'll try them out. Light the point of this question was I could not see any visible error because it's a logic error.

0

kingdom could you elaborate on me loading the array? I can't see how I would solve this

1 answer

Try this? It waits 3 seconds to wait for the game to shut down if that doesn't work then tell me. Also waits for the data to load!

local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency')
local RunService = game:GetService('RunService')

game.Players.PlayerAdded:connect(function(player)
if not RunService:IsStudio() then
Parent:WaitForDataReady()
end
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"
    local Schmeckles = Instance.new("IntValue", stats)
    Schmeckles.Name = "Schmeckles"

    local key = "player-"..player.userId

    local SavedValues = DataStore:GetAsync(key)

    if SavedValues then
        Schmeckles.Value = SavedValues[1]
    else
        DataStore:SetAsync(key, Schmeckles.Value)
    end
end)


    game.Players.PlayerRemoving:connect(function(player)

    local key = "player-"..player.userId

    local Schmeckles = player.leaderstats.Schmeckles.Value
    DataStore:SetAsync(key, Schmeckles)


    end)

game.OnClose = function()
wait(3)
end


7 comments

0

Hello, thanks for writing a script that was oddly generous. I'm afraid in line 06 you've left Parent undeclared, what did you intend on putting beforehand?

0

Oh sorry, Parent is suppose to be "player" lmao opss

0

aha! Thanks, I'll let you know if it functions

0

Sorry! I'm afraid this one doesn't even let the instance 'Schmeckles' appear in leaderstats

0

Update: Managed to get it working again however will not save overall, Il test it later with two players when the game isn't closing

0

Oh, okay anymore questions?

0

well, apart from the fact it just doesn't save - am I just putting it in the wrong place?

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