DataStore Value not Saving

You can always browse the forums and other sources to help you.

Here’s a youtube video you can watch https://www.youtube.com/watch?v=4Dc_bri9mjs

Thank you for the Tutorial, i will try it out again… if i still dont make any progress with it do you have any other suggestions?

You could try practicing it in studio and try to understand some of the important functions and events, like FireServer(), FireClient(), OnServerEvent, and OnClientEvent, which are explained on the API ref. page of RemoteEvents here.

You can look around the devforum and many other places for examples of how to use RemoteEvents/Functions. If you ever have any issues with it, you can always come back here and you’ll be assisted

Thanks first of All for the Huge Support so far. I am trying to get the game done for my Christmas Stream on YouTube. So i am trying to understand and learn it relatively fast. Well since Christmas its only 13 Days until Christmas. Currently The only big issue that has happened is with the RemoteEvents for saving. I did use RemoteEvents as an example for my Quest system in general. But the saving process (giving Values back to the server Script) i just dont seem to understand

I tried the saving Process with the Help of the video but i still don’t really seem to get how the saving would work. This is for me the first time of trying to learn how Roblox Studio in general works, I’ve only build before but this time i wanted to make a nice game for Christmas.

What you basically need to do is when you want to change the value, you will need to make a function for the remote’s OnServerEvent event in a normal script.

-- It should look something like this
local event = game.ReplicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(player, value)
    --[[
        "player" is the player who called FireServer for the server
        on their client: it always the first parameter of OnServerEvent
    --]]
    
    -- change the value on the server here [this is just an example]
    playervalue.Value = value
end)

When we want to change the value on the server, we’d want the client to fire the event and call the function connected to the OnServerEvent event in the normal script. We do this by calling FireServer.

-- //In a local script\\
event:FireServer(true) -- where the true is, you'd have to put the desired value to change it to. Whether it be, true or false

And there, the server will see the change and will proceed to save

One thing I always suggest when working with datastores is to implement a BindToClose function to save everyone’s data before the server shuts down. BindToClose makes sure the function that is passed is called and finished running before the server shuts down. BindToClose gives a maximum of 30 seconds for the function to be completed before the server is shut down. After the 30 seconds, the server will be shutdown, regardless of the function not being completed

game:BindToClose(function()
   for _,player in pairs(game.Players:GetPlayers()) do
        -- save data for each individual player here
   end
end)

The function above basically iterates through the player list and proceeds to save their data. The request may be throttled, but will be saved as the request will be handled eventually

That sounds very smart to use that function!
By the way is it somehow possible for me to share my code here? Or would that be a bad idea?

Sorry I am also not very experience with this Forum here, i was promoted today xd…

You can post your code here - it’ll make it easier for me to assist. You should also know that you can format the code in codeblocks to make it readable. Here is how you make one:

codeblock

This is a codeblock

Haha, xd. We were all there once :smile:

the Value used in the Local Script is from Collecting Items
The Quest is : Find two items each item highers the Value by one when it was clicked
If the Value is >= 2 then you get your reward

This part will not be replicated to the server, only the client will see the change, since it is changed on the client and respecting the rules of FilteringEnabled. Here is where you need to use the RemoteEvent. You can refer to my post above about making this

Also, try to implement the BindToClose function I mentioned: it’ll help to reduce data losses in your game

would that look like this ?

local playr = game.Players.LocalPlayer
local badges = playr:WaitForChild("Badges")
local event = game.ReplicatedStorage.RemoteEvent

while(1) do
	wait(1)
	if script.Parent.Value.Value >= 2 then
		----- 
	    game.ReplicatedStorage.Reward:FireServer()
			local badges = playr.Badges
			event:FireServer(badges.Quest1.Value = true)
	wait(2)
	script.Parent.Visible = false
	script.Parent.Value.Value = 0
	script.Disabled = true
	wait(0.5)
	script.Disabled = false
	

		
		end	

No, it should look like this: event:FireServer(true)

Also, an update to the OnServerEvent part of the RemoteEvent, I have now changed it to change any value, as long as the object is replicated on the server:

event.OnServerEvent:Connect(function(player, value, new_value)
    --[[
        "player" is the player who called FireServer for the server
        on their client: it always the first parameter of OnServerEvent
    --]]
    
    -- change the value on the server here [this is just an example]
    value.Value = new_value
end)

With that, that snippet of code you gave should actually look like is this now:
event:FireServer(badges.Quest1, true)

I will try now the live test, it does save :smiley: in Roblox Studio. Now i will try to save it on the live game and rejoin to see if it fully saves now

Dude thank you so much it finally works :OOO

I’d recommend not using BindToClose, as it’s very broken and not 100% cooperative. I would do periodic saves, maybe every 2 minutes or something, but in terms of saving only on bindtoclose is not recommended.

Hello, thank you as well for sharing this with me. I am very very new to scripting and every little bit of help is so appreciated !

2 Likes

Oh, I wasn’t aware, thanks for letting me know. An autosave would be good

2 Likes