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

datastore is not saving data?

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

local player = game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local collectibles = Instance.new("IntValue")
	collectibles.Name = "Collectibles"
	collectibles.Parent = leaderstats
	collectibles.Value = workspace.Collectibles.Value
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	level.Value = 1
	
	player.RespawnLocation = workspace.Level1.L1Spawn
	
	local playerUserID = "Player_"..player.UserId
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserID)
	end)
		
	if success then
		collectibles.Value = data
	end
		
end)

game.Players.PlayerRemoving:Connect(function()
	local playerUserID = "Player_"..player.UserId
	
	local data = player.leaderstats.collectibles.Value
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserID, data)
	end)
		
	if success then
		print("data saved")
	else
		print("data not saved")
		warn(errormessage)
	end

end)

10 comments

0

Are there any errors in the output, and if so, what line is throwing the error?

0

no errors

0

Also, could you explain your problem in more detail instead of saying it doesn't work and posting your script?

0

the value isnt saving

0

remove the local player = on line 4 as it is pointless

0

i tried again and got this error "UserId is not a valid member of RBXScriptConnection"

0

How are you testing whether or not it saves? If you play the game and manually edit your intvalue, it won't save. However, if you write up a script that changes to intvalue, it does save.

0

i have a collectible coin that I tried picking up

0

Also, you should add "player" as a parameter for the leave function

0

As akatak_prime said add in the player paramater as your code will try and read the varable on line 4. Lua will take the variable that has the closest scope if multiple variable with the same name exist.

2 answers

I fixed the problem, was just some minor capitalization errors on my part.

First of all Remember to turn API Services on, so you need to publish the game and go to game settings and turn API Services on

Second of all it does not work in roblox studio

Third of all your good to go!

3 comments

0

DataStore very much does work in studio.

0

it doesn't seem to be working in game either

0

it works in studio.

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