How to save a local value in a data store

heres the issue, i have a local number value that changes locally, and when i data store it in the server, the server gets confused because it sees it as a 100 even though in the local i change it to 50 , but because the server dosent see any changes in the client it sees the defualt value which is 100 not the changed value, so my question is how can i send the local number value changes from the client to the server when the player leaves and data store it??

3 Likes

by changing in the local are you saying that you go into the explorer and change the value while playing the game?

2 Likes

No, by a local script in the player gui

1 Like

firstly, do you have the code? and secondly, the values that the script changes while playing the game will not save when you press stop

1 Like

heres the code even though it think theres some mistakes considering i just now started learning data stores,

local DataStoreService = game:GetService("DataStoreService")

local PlayerNeedsDataStore = DataStoreService:GetDataStore("PlayerNeedsDataStore")

local DatasToClient = game.ReplicatedStorage.DatasToClientRM
game.Players.PlayerAdded:Connect(function(plr)
	
	local PlayerNeeds = Instance.new("Folder")
	
	PlayerNeeds.Name = "PlayerNeeds"
	PlayerNeeds.Parent = plr
	
	local Energy = Instance.new("IntValue")
	Energy.Name = "Energy"
	Energy.Parent = PlayerNeeds
	Energy.Value = 100
	
	local PlayerUserId = plr.UserId
	
	local Data
		
	local Success, ErrorMessage = pcall(function()
	
		Data = PlayerNeedsDataStore:GetAsync(PlayerUserId)
		print(Data)

	end)

	if Success then
		
 		Energy.Value = Data

		DatasToClient:FireClient(plr,Data.Energy)
		
		print("PlayerNeeds have been successfully Loaded")

	else

		warn(ErrorMessage)

		print("there was an error while loading data")

	end
	
end)

game.ReplicatedStorage.DatasToServerRM.OnServerEvent:Connect(function(player, EnergyVal)

	local Success, ErrorMessage = pcall(function()

		PlayerNeedsDataStore:SetAsync(player.UserId, EnergyVal)

	end)
	
	if Success then
		
		print("PlayerNeeds have been successfully saved")
		
	else
		
		warn(ErrorMessage)
		
		print("there was an error while saving data")
		
	end

	
end)

and the local script

local DatasToClient = game.ReplicatedStorage.DatasToClientRM

local player = game.Players.LocalPlayer

DatasToClient.OnClientEvent:Connect(function(EnergyValue)

local function EnergyRemoval()


	repeat
		
		local randomwait = math.random(60,100)
		wait(1) 

			script.Parent.Parent.EnergyBar.Size = UDim2.new(EnergyValue/ 22 ,0 , 0.693, 0,0)
		
			EnergyValue -= 5
		
		until EnergyValue <= 0
	
	repeat
	
	wait(0.0001)

		until EnergyValue > 0
	
	EnergyRemoval()
	
end
	
	EnergyRemoval()
	
	end)

game.Players.PlayerRemoving:Connect(function()
	
	game.ReplicatedStorage.DatasToServerRM:FireServer(player, game.Players.LocalPlayer.PlayerNeeds.Energy.Value)
	
end)

1 Like

you might not have correctly referenced the energy value. find the location of the energy value and do this for example

EnergyValue.Value -= 5

im pretty sure this is why the local script cant change the value of the IntValue

1 Like

the “EnergyValue.Value” is the value sent to the client by the server in

so logically it would have changed the value sent by the server

1 Like

also when you use FireServer() you only have to put in the arguments. OnServerEvent() is the one that requires the player first then the rest of the arguments. for example:

FireServer(arg, arg, arg)
OnServerEvent(plr, arg, arg, arg)

1 Like

i dont know if my right, but one of my argument are actually the player, i want to send the playeruserid to the server and then in the server script save that specifc player’s data,

1 Like

Isn’t Data.Energy the value that you datastored?

yes , it is the value that i have datastored

oh so player is storing the userid of the player and youre sending that over? if so then you dont need that because the player is already sent over so on the server side script you can use

Players:GetUserIdFromNameAsync( [username of player] )

to find the userid

my problem is that how i can send local values to the server, when the player leaves, do i use a remote event or something else?

By doing EnergyValue -= 5 you are changing the value of the variable and not the real value in the explorer. you have to locate the value in explorer, assign it to EnergyValue and change the value using EnergyValue.Value -= 5

(remember to change every EnergyValue to EnergyValue.Value or it wont function properly)

ok i will try that and see thanks so far for the help

alright can you show me the heirachy of your game and the local values? because im pretty sure the server can see literally everything so there shouldnt be any need to use a localscript to tell the server

sorry for not sending the heriachy but heres the changes i made

local DatasToClient = game.ReplicatedStorage.DatasToClientRM

local player = game.Players.LocalPlayer

DatasToClient.OnClientEvent:Connect(function(EnergyValue)

local function EnergyRemoval()


	repeat
		
		local randomwait = math.random(60,100)
		wait(1) 
			

			script.Parent.Parent.EnergyBar.Size = UDim2.new(player.PlayerNeeds.Energy.Value/ 22 ,0 , 0.693, 0,0)
		
			player.PlayerNeeds.Energy.Value -= 5
		
		until player.PlayerNeeds.Energy.Value <= 0
	
	repeat
	
	wait(0.0001)

		until player.PlayerNeeds.Energy.Value > 0
	
	EnergyRemoval()
	
end
	
	EnergyRemoval()
	
end)

but something weird happned, The Energy value in the player is 0 when it goes to the client, but in the server script i made it 100, so idk why its becoming 0

here you put randomwait and it seems to do nothing is that intentional?

yeah lol , i know it dosent do anything its just there when im done with the script, i change the wait(1) to wait(randomwait)

i think the value is becoming 0 because youre reducing it with EnergyValue.Value -=5. is it gradually decreasing or just jumps to 0?