DataStore Value not Saving

Hello, I’ve recently started working on my first Roblox Game.
The game is a Collecting Game, where you collect Items / do Quest and Collect “Badges”.
So far I’ve created with the help of my friend a DataStore.
All of the Values are saving (they are located in Items in the Workspace), besides the one from my Quest System.
The Value for that is changed in a LocalScript in the StarterGui.
I do not know how i could return the values from the LocalScript back to my Server script where the values are saved.

2 Likes

Use a remote event to fire to the server.

You can read up on remote events here; you’ll want to look up the :FireServer and :OnServerEvent Events.

1 Like

You’re changing the value in a localscript: it will not be replicated to the server. You need to use a RemoteEvent for this

1 Like

I have tried earlier to create a RemoteEvent to solve the issue, but since I’m really new to Lua i didn’t really figure out how it works. I will try it again with the Help Page you included

Okay, so I’ve tried to use RemoteEvents, but i still have Problems actually understanding how they work. Which might be the reason why it is still not working.

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)