Client to update server issues

I made a stats update Gui to display and change values local to a player. The server doesn’t seem to save them between sessions and I can’t call the changes when referencing them via a coin collection script. Joined game with 1 rebirth point and placed it into “Increased Coins”. Am I supposed to do something with the DataStoreService when altering values inside a player from a local script?

Client sees - BonusCoins.Value = 1
Capture1

Server sees - BonusCoins.Value = 0

---- Increase and decrease the bonus coins value for the player ----
local player = game.Players
local textLabel = script.Parent
local bonusCoins = player.LocalPlayer.playerstats.BonusCoins
local upButton = script.Parent.Parent.UpButton
local downButton = script.Parent.Parent.DownButton
local rebirthToken = player.LocalPlayer.playerstats.RebirthToken

upButton.MouseButton1Click:Connect(function()
	if rebirthToken.Value ~= 0 or nil then
		rebirthToken.Value = rebirthToken.Value - 1
		bonusCoins.Value = bonusCoins.Value + 1
		task.wait(0.01)
	end
end)

downButton.MouseButton1Click:Connect(function()
	if bonusCoins.Value ~= 0 or nil then
		bonusCoins.Value = bonusCoins.Value - 1
		rebirthToken.Value = rebirthToken.Value + 1
		task.wait(0.01)
	end	
end)

local function updateText()
	if bonusCoins.Value ~= 0 or nil then
		textLabel.Text = bonusCoins.Value
	else
		textLabel.Text = 0
	end
end

bonusCoins:GetPropertyChangedSignal("Value"):Connect(function()
	updateText() -- Update as the value changes
end)

updateText()

Is this a local or server script?

you need to communicate these changes with the server, otherwise it wont know it happened. make some remoteevents

Well it’s a Gui so local script

Have you tried creating a remote event? You can fire that remote event to the server and it should add it to the server-sided stats.

1 Like

Yeah I’m looking into that now. Not 100% sure whether the if/then statements stay where they are or if they go into a server script and the client just fires the OnServerEvent or not.

1 Like

I do not think you will need to detect things on the client as exploiters do exist, but I’m certain you can just change those if statements server sided.

My first attempt has me perplexed.
image

local player = game.Players.LocalPlayer
local playerstats = player:WaitForChild("playerstats")
local bonusCoins = playerstats.BonusCoins
local rebirthToken = playerstats.RebirthToken
local upButton = game.PlayerGui.StatsGui.Frame.InnerFrame.CoinsText.UpButton
local downButton = game.PlayerGui.StatsGui.Frame.InnerFrame.CoinsText.DownButton
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local function upButton()
-- some code
end

local function downButton()
-- some code
end

remoteEvent.OnServerEvent:Connect(upButton)
remoteEvent.OnServerEvent:Connect(downButton)
1 Like

Having the same remote event connected to downButton will disconnect the connection to upButton connection.
Also, the client should NEVER be making changes to how much coins they have, the server should. When the client presses a button, fire a remote event for the server to change that and add coins, only on click. And the server should simply replicate the values to the client, via a leaderstats folder value or some other way of replicating values.

TLDR: I highly highly recommend you to not put your data saving/ setting/ managing scripts anywhere but in ServerScriptService, for security reasons.

PS: You can’t use OnServerEvent in a localscript.

Try decreasing it to 0 instead of one.

Yep it is in the ServerScriptService as you can see from the error. The GUI in question is in StarterGui (Which I believer gets cloned to each connected player into PlayerGui) so no issues there. Your remote event comment is helpful but I’m having trouble calling the playerstats (a folder in the same location as the leaderstats folder, player.LocalPlayer.playerstats). Usually you waitforchild() but in this case it says infinite yeild possible in the output window and when I play around with it I get errors. “ServerScriptService.PlayerStatsScript:2: attempt to index nil with ‘WaitForChild’”

Sorry, but if this is all server side then…
None of that will work.

There’s also no such thing as game.PlayerGui, PlayerGui is within a player.

It’s all working now. Local talks to server and updates coins value correctly. Thx all for comments.