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

Data store service script doesn't work?

Hello!

I've been working on a data store script, but when I changed my cash value and test played again, it didn't work. I've checked the script for errors, and there are none, it also didnt print anything in the output, I have API Services on, and still dosent work, even published it.

Here's script:

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

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Points"
	
	local playerUserId  = "Player_"..plr.UserId
	print(playerUserId)
	
	--Loads data
	
	local data
	local success, errormessage = pcall(function()
	 data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		--Set data equal to current cash
		cash.Value = data
	end
	
end)
	
game.Players.PlayerRemoving:Connect(function(plr)
	local playerUserId = "Player_"..plr.UserId
	print(playerUserId)
	
	local data = plr.leaderstats.Points.Value
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Data saved")
	else
		print("Save error")
		warn(errormessage)
	end
end)

2 answers

✓ Accepted answer

Even if it does load the data succesfully, it doesn't mean the data is a integer. It could still be nil. Try checking if it's not nil first.

if success and data ~= nil then
        --Set data equal to current cash
        cash.Value = data
end

Before you test this, make sure to start with a new datastore to prevent loading in previous data.

local myDataStore = DataStoreService:GetDataStore("myDataStore2")

Hope this helps :D

27 comments

0

Lemme try that really quick.

0

Erased the old Mydatastore and replaced it, and replaced the if statement too

0

Did it work?

0

Tried it. Didn't work, I changed the points value too

0

Are you trying this in studio?

0

Yes.

0

Try doing a live test, sometimes studio is buggy

0

Ya mean join in client, not studio?

0

Are you changing the points value from the client side or server side. Client side won't save.

0

I changed the value from the server side in studio

0

But did you meant try it in roblox client or studio?

0

Okay, try and do a live test

0

Roblox client

0

Ohh, I understand you now.

0

kkkkkk

0

Still didn't work in roblox client, didnt try it in studio. Maybe DataStoreService is down?

0

That'd be extremely bad for every roblox game, so I'm doubting that

0

I know, but then how does the script dosen't work? I checked for errors, but 1 red flag is nothing printed in output

0

Replace line 35 with this (Idk my gut is telling me something): myDataStore:SetAsync(playerUserId, plr.leaderstats.Points.Value)

0

Nothing.

0

Well ,if someone wants to give up right now, we can probably agree on a point that DataStoreService is down.

0

rip

0

Well, if you want to give up, well, thank you for trying.

0

Do you mind putting the full script and your exact method of trying it?

0

Alright, I first tested it with studio client, then tested it on roblox client, then nothing happened, I changed the points value by explorer in studio, then developer console in roblox client. Full script is below

0

How are you changing your cash value exactly?

0

Conclusion: Data stores don't work in studio, even if api services is on....

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

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Points"
	
	local playerUserId  = "Player_"..plr.UserId
	print(playerUserId)
	
	--Loads data
	
	local data
	local success, errormessage = pcall(function()
	 data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success and data ~= nil then
		--Set data equal to current cash
		cash.Value = data
	end
	
end)
	
game.Players.PlayerRemoving:Connect(function(plr)
	local playerUserId = "Player_"..plr.UserId
	print(playerUserId)
	
	local data = plr.leaderstats.Points.Value
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, plr.leaderstats.Points.Value)
	end)
	
	if success then
		print("Data saved")
	else
		print("Save error")
		warn(errormessage)
	end
end)

5 comments

0

Get this: I copied the exact script, put it in my game, ran a live test and it works fine. Here it is: https://www.roblox.com/games/4589359421/uhSaxlras-Place-Number-160

0

games not public, but i am glad you got it working

0

WHAT HOW THAT IS IMPOSSIBLE

0

Hold up

0

Public

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