Recently, I made a post about a Datastore problem, someone helped me and it was working fine, only on single player
My datastore code is basically saving a boolean value, so if it is set on true then when the player rejoins it would be set on true. The default for this is false.
I tested it within studio, and it the problem goes something like this: player1 has the boolean on true and player2 has the boolean on false. I disconnected the server and relaunched it to see if it saved. For player1 the boolean was correctly set on true but for player2, it was incorrectly set on true as well.
Is there any errors in the code that I potentially put so this would occur? Thank you for reading.
In the save script you are saving your data as a table. So when it loads and you run the code:
if data then
It is actually checking if the table is not equal to nil, but not the SpeedCheck value itself. To fix this, either save it as a boolean and not a table in the Save function, or change the load line to:
SpeedCheck.Value = data and data.SpeedCheck
which is also a nice one line solution.
Also some notes:
- When you Connect the PlayerAdded and PlayerRemoving Event to a function, instead of doing:
game.Players.PlayerAdded:Connect(function(player)
onPlayerJoin(player)
end)
You can instead do
game.Players.PlayerAdded:Connect(onPlayerJoin)
-
When sending code, use 3 backticks (```) and then your code, ending with another 3 backticks. This means its formatted in the website and easier to see and modify
-
When using Datastores, wrap any call to the datastore in a pcall, so that if it ever has an error, you can spot it and kick the player so that no data is lost.
Thanks! The whole entire script works now because of that one code.
I make sure to use that 3 backticks advice of yours next time I make a post
When Sending a Request to Get / Save Data, you need to wrap the Request in a pcall()
to prevent errors from popping up, so you can handle it properly which would look like this:
local Success, Response = pcall(function()
return PlayerData:SetAsync(Key, Data)
end)
-- 'Success' Determines if the operation was successful
-- 'Response' can be either the Data that was Returned, or an Error if It fails
But, we can simplify this pcall()
, a unknown part some functions is that we can change a Colon, into a Period, like this:
DataStore:SetAsync() -- There is a Colon for that function
DataStore.SetAsync() -- There is a Period in this function
What this changes, is that you now have to apply self
manually (this is a OOP thing), for this case, we can just provide the DataStore
as itself, and Set the Following:
DataStore.SetAsync(DataStore, Key, Data)
Which for the pcall()
. we can apply like this:
pcall(DataStore.SetAsync, DataStore, Key, Data)
Which would more so be the “proper” way of doing it.
Forgot Something?
data = data or {}
This is Actually Correct for what he did, there is nothing actually wrong with it, you do this if you intend to add multiple Arguments inside something
Sorry but this post sounds a little passive aggressive. If you’d like to correct me, please do, i’m open to being criticised. Just please don’t talk to me like I don’t know what i’m doing
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.