My datastore script doesnt work

My datastore script doesnt work help

local DataStoreService = game:GetService(‘DataStoreService’)

local ClanDataStore = DataStoreService:GetDataStore(‘ClanDataStore’)

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

local SpinLeftText = player:WaitForChild('PlayerGui'):WaitForChild('Menu').CharacterDesignFrame.SpinLeft
local ClanText = player:WaitForChild('PlayerGui'):WaitForChild('Menu').CharacterDesignFrame.ClanLabel

local data
	data = ClanDataStore:GetAsync(player.UserId..'-clandata')

	SpinLeftText.Text = data.SpinLeft
	ClanText.Text = data.ClanData

end)

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

local Clandata = {SpinLeft = player.PlayerGui.Menu.CharacterDesignFrame.SpinLeft.Text, ClanData = player.PlayerGui.Menu.CharacterDesignFrame.ClanLabel.Text}

	ClanDataStore:SetAsync(player.UserId..'-clandata', Clandata)

end)

image

I think you will need to check that data isn’t nil before trying to access it. Any new player that joins that doesn’t have save data will throw this error.

1 Like

I get no errors but it still doesnt work

Try this and see if you get the error in the output window. If so then there is no data for that user stored in the datastore.

What if there is no data stored in the player ? How can i fix it

Are you just running this in Studio? I’ve often found when play testing in studio that it doesn’t usually fire game.Players.PlayerRemoving. For Datastore testing purposes I usually just make a gui button that I can press to fire the server and save player data to the datastore.
If your playing this in a published game then you will need to go over your datastore code as something must be incorrect if it isn’t saving.

As a general rule, I would suggest you learn how to use Datastore2. It’s quite a bit easier and safer than Datastore.

There is a plugin called “Datastore Editor”, and it’s amazing.

I tried it in game too.

And I think i found the problem

So the problem is;

The player removes from the game so fast
and the game cant find playergui because its destroyed and I dont know how to fix it any ideas?

(NOTE: I keep getting ‘Couldnt Find PlayerGui’ Errors)

I would suggest that you don’t save data from the playergui. Save the data somewhere on the server, then replicate it to the client. That way, you have control over when the data is going to disappear.