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

DataStores won't save?

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

game:GetService("Players").PlayerAdded:connect(function(plyr)
	local bank = "user_"..plyr.userId --Using the player's name glitches out somehow.
	while wait(180) do
		if plyr.PlayerGui.MoneyGui:WaitForChild("Money") then
			plyr.PlayerGui.MoneyGui.Money.Text = "$"..tostring(plyr.PlayerGui.MoneyGui.Money.Text:sub(2)+5)
		end
	end
	wait(.5)
	plyr.PlayerGui.MoneyGui.Money.Text = Money:GetAsync(bank)
	print("Loaded Your Money")
	plyr.CharacterAdded:connect(function(char)
		wait(.5)
		char.Humanoid.Died:connect(function()
			Money:SetAsync(bank, plyr.PlayerGui.MoneyGui.Money.Text)
			print("Saved Your Money")
		end)
		plyr.PlayerGui.MoneyGui.Money.Text = Money:GetAsync(bank)
		print("Loaded Your Money")
	end)
	plyr.PlayerGui.MoneyGui.Money.Changed:connect(function()
		Money:SetAsync(bank, plyr.PlayerGui.MoneyGui.Money.Text)
		print("Saved Your Money")
	end)
end)

It won't print anything or save anything. When I reset my character , the gui doesn't change to the amount of money you have saved. Please help!

2 answers

✓ Accepted answer

It's because you put an infinite loop halfway through the script... When you put:

while wait(180) do
...
end

You basically make it so every 180 seconds ... happens.. which breaks the script. Nothing after that code will be executed.

To fix this, you can move that loop to the bottom of the script (before the last end)) or use a separate thread for it.

4 comments

0

It doesn't "break" the script, else the "while" loop will not run at all. Rather, the thread is stuck in the loop forever.

0

I would have to do a spawn function. mkay thx. It's supposed to loop infinitely.

0

you could create a coroutine for the while loop

0

@Lord @Future That's what I meant when saying "use a seperate thread". The Spawn function creates a new thread, and so do coroutines.

Did you try in-game? Sometimes studio is weird about that stuff.

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