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

HELP WITH DATA STORES? [WONT WORK, PLEASE HELP!]?

ERROR: Attempt to index local 'savedValues' (a number value)?

DataStore = game:GetService("DataStoreService"):GetDataStore("PeasStore")


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

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

local points = Instance.new("IntValue", stats)
points.Name = "Points" 

local coins = Instance.new("IntValue", stats)
coins.Name = "Coins" 

local key = ("player-" .. player.UserId)
local savedValues = DataStore:GetAsync(key)

if savedValues then
    points.Value = savedValues[1]
    coins.Value = savedValues[2]
else
    local valuesToSave = {points.Value}
    DataStore:SetAsync(key, valuesToSave)





end

end)

Error at line 19

``PLEASE HELP ME!

9 comments

0

Line 22. You make a table with curly braces {} not brackets ()

0

Still doesnt work.

0

try dis: points.Value = savedValues[1] , coins.Value = savedValues[2]

0

That is what I wrote..

0

No commas... See before you judge

0

So? Did it work or what?

0

Hello??

0

I will try. But how will I write it?

0

I wrote it with commas, and on the same line. But I just got an error. Thanks for the help anyways! :)

1 answer

You need to save when the player leaves the game too. I also added some things to the script that will help.

Try this:

-- Define variables
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

-- You should put your data store as a separate variable then the service one 
local DataStore = DataStoreService:GetDataStore("NameHere")

-- Loads the data back to the player
local function Load(plr)
	-- Creates the stats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

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

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

	-- Don't give a value yet
	local SavedStats
	local Key = "plr-"..plr.UserId -- key

	pcall(function() -- Stops errors
		-- Data Stores can error so thats why I did this
		SavedStats = DataStore:GetAsync(Key)
	end)

	-- Gives the player the data if they have data else create a datastore for them
	if SavedStats then
		points.Value = SavedStats[1]
		coins.Values = SavedStats[2]
	else
		local Values = {
			points.Value;
			coins.Value
		}

		pcall(function() -- Stops errors
			DataStore:SetAsync(Key, Values)
		end)
	end
end

-- Saves on leaving
local function Save(plr)
	local Key = "plr-"..plr.UserId

	local Values = {
		plr.leaderstats.Points.Value;
		plr.leaderstats.Coins.Value
	}

	pcall(function() -- Stops errors
		DataStore:SetAsync(Key, Values)
	end)
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

I know it may look intimidating since it has 63 lines but it's just an upgraded version to what you had. Also make sure you have datastore enabled for your place. If you don't you can enable it by going to your place and clicking on the tree dots. Next press configure this place. Click on Games. Then press the Allow this place to be updated using the Save Place API in your game. checkbox.

Hope this helps!`

6 comments

0

Could you explain what the ';' does at line 53? Thanks!

0

Semi-colon

0

The semi colon does the same thing as a comma in this case. You can use commas instead of the semi colon (in this case).

0

Ok thanks!

0

Could you please edit out the pcalls, they seem to not work for me. Then I will accept the answer, thanks!

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