DataStore not working

I’ve attempted to try and do a data save code, but the data would not save and I don’t know why

This is my first time attempting this without any help and I have no idea what is wrong with it, so can anyone spot any mistakes within the code? Thank you for reading

2 Likes

Have you tried this?

game.Players.PlayerAdded:Connect(function(Player)
    onPlayerJoin(Player)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    onPlayerLeave(Player)
end)

send the formatted code so I can easily help you :slight_smile:

no, it still wouldn’t save data

the variable “data” is a table, so it looks like this:
local data = {}

so when you set the value of the boolvalue to data you are setting to a table, which isn’t possible.

so instead you should do

SpeedCheck.Value = data.SpeedCheck

also when saving you should do this instead:

this wont work because of how your code is written, this is an example of how it would look.

local function onPlayerLeave(Player)
	local succ, err = pcall(function()
		local playerUserId = "Player_".. Player.UserId
		local speedcheckValue = Player.BlackMarketThings.SpeedCheck.Value

		local dataToSave = {
			SpeedCheck = speedcheckValue,
	}
		playerData:SetAsync(playerUserId, dataToSave)
	end)
	if not succ then
		warn("Could not save data! Error message:", err) -- will tell you the error
	end
end

You need a way to get their SpeedCheck value, the code I provided wont work because it assumes that the BlackMarketThings folder is in their player

1 Like

But you can rewrite yours with these tips:

  • when saving data, make sure you are saving a table like the code I provided
  • you need a way to access that player’s SpeedCheck value. I’d put the value inside of their player.
    (because yours creates a new value for each player that joins but they are all named the same, so you end up with 30 values in BlackMarketThings with the same name and no way to know whos value is who’s)
  • delete their SpeedCheck value after they leave

Thanks for your tips! From the code you provided earlier the whole thing just decided to work again

it might work now, but it shouldn’t work when more than 1 person is in the game. (if I’m not mistaken)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.