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

Why is this Datastore not working? (Answered)

Hello. Currently i am want to set up a Build/Adventure game, where you have to fight agains NPC and mine Ores for getting a Income to Build Buildings. I have set up 2 Scripst for that.

PlayerEntered

local DataStore = game:GetService("DataStoreService"):GetDataStore("CoreStorage")

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

	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"

	local OreMass = Instance.new("IntValue", stats)
	OreMass.Name = "Mass"
	
	local Bkills = Instance.new("IntValue", stats)
	Bkills.Name = "Botkills"
	
	local mined = Instance.new("IntValue", stats)
	mined.Name = "Oresmined"
	
	local key ="player-"..player.userId
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		--Save format: (Bkills, mined)
		Bkills.Value = savedValues(1)
		mined.Value = savedValues(2)
	else
		local valuesToSave = {Bkills.Value, mined.Value}
		DataStore:SetAsync(key, valuesToSave)
	end

end)

The Values called Bkills and mined should Save, when the Player is leaving.

Script 2 Player Leaving

l~~~~~~~~~~~~~~~~~ local DataStore = game:GetService("DataStoreService"):GetDataStore("CoreStorage")

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

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

--Save key:(Bkills, mined)
local valuesToSave = {player.leaderstats.Botkills.Value, player.leaderstats.Oresmined.Value}
DataStore:SetAsync(key, valuesToSave)

end)


But it wont. So i ask, what did i made wrong.



1 comment

0

Also sry for my Bad Grammar. I am not that good in speaking English.

1 answer

✓ Accepted answer

When checking for values in a table, square brackets '[]' should be used, not parentheses. On lines 23 and 24 you used parentheses.

local DataStore = game:GetService("DataStoreService"):GetDataStore("CoreStorage")

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

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local OreMass = Instance.new("IntValue", stats)
    OreMass.Name = "Mass"

    local Bkills = Instance.new("IntValue", stats)
    Bkills.Name = "Botkills"

    local mined = Instance.new("IntValue", stats)
    mined.Name = "Oresmined"

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

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        --Save format: (Bkills, mined)
        Bkills.Value = savedValues[1]
        mined.Value = savedValues[2]
    else
        local valuesToSave = {Bkills.Value, mined.Value}
        DataStore:SetAsync(key, valuesToSave)
    end
end)

Other than that, it looks alright. Edit your question if you have any other issues. Hope this helped!

2 comments

0

Now its working. Thx for the fast Help.

0

No problem.

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