Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Updating a value server-side?

Alright so, here goes:

I have a local script that sets a series of values based on sums and with what the user has put inside of a textbox.

When the user hits the button, it is meant to take that amount that the user has put in the textbox, set it to a value, then set another value to what it already has (example 10) minus what value in the textbox/value was, let's say it was 5.

So then once that's done, if the other checks are successful, it sets another value which has information from a datastore to that information subtracting the amount the user has put in, then putting in that amount that was in the textbox inside of another textlabel for the user to see.

In more simplistic terms, it's an ATM machine. User puts in the amount they want to take out and if they have equal to or greater then the amount they want to take out, it lets them take that amount out and then sets however much money they have left including what they just took out, then the money they just took out goes into a textlabel for "cash on hand"

My issue is that when I try to set the main value holding how much money the user has in their bank, the server doesn't listen. It is set locally no problem, but then when the server comes to update it (when the user gets paid from their in-game job) it uses the old value before any transaction was made, + their salary.

Below is snippets of how the code works:

ReplicatedStorage = game:GetService("ReplicatedStorage")
WithdrawFunction = ReplicatedStorage:WaitForChild("WithdrawMoney")


function leftClick()
 for i, player in pairs(game.Players:GetChildren()) do
     local Money = player.Money -- amount of money in the bank
     local cash = player.Cash -- amount of money they have in cash.
     local amount = script.Parent.Parent.Amount -- the textbox they put the amount in
     local WithDrawAmount = script.Parent.WithdrawAmount -- the value that gets set to the amount
     local CashGUI = player.PlayerGui.MouseOverGui.Frame.CASHBAL -- the user's visible text label for how much cash they have
     local BankGUI = player.PlayerGui.MouseOverGui.Frame.BANKBAL -- the same, but for how much money they have in their bank
     WithDrawAmount.Value = amount.Text -- sets the withdraw amount value to how much they wanna take out
     print("set withdraw amount to " .. WithDrawAmount.Value)

     if Money.Value >= WithDrawAmount.Value then -- checks to make sure the user has enough in their bank to take the money out in the first place
	 CashGUI.Text = "CASH ON HAND: $" .. WithDrawAmount.Value -- sets the visible text label to the amount of cash they got out
     Money.Value = Money.Value - WithDrawAmount.Value -- sets the existing bank money to how much they should have including how much was taken out
     print("Success! Withdrew and set bank bal to " .. Money.Value)
     cash.Value = WithDrawAmount.Value -- volatile storage value storing how much cash they have, co-incides with the cashgui.text
     BankGUI.Text = "BANK BALANCE: $" .. Money.Value -- sets their new bank balance according to the value
     WithdrawFunction:FireServer(Money.Value) -- fires to the server so that it can set all that information up on the other side
		else
	print("not enough poor")
end
      
    end
    end
script.Parent.MouseButton1Click:Connect(leftClick)

Then this is the server script, in serverscriptservice, which should then set the value correctly serverside:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local WithdrawSuccess = ReplicatedStorage:WaitForChild("WithdrawMoney")



WithdrawSuccess.OnServerEvent:Connect(function(Player, Text)
     local Money = Player.Money
     local cash = Player.Cash
     local WithdrawScreen = Player.PlayerGui.CommBankATM.Withdraw
     local amount = WithdrawScreen.Amount
     local WithDrawAmount = WithdrawScreen.Withdraw.WithdrawAmount
     local CashGUI = Player.PlayerGui.MouseOverGui.Frame.CASHBAL
     local BankGUI = Player.PlayerGui.MouseOverGui.Frame.BANKBAL
print("Server-side is active @Blue_Heelers!")
Money.Value = Money.Value - WithDrawAmount.Value
print("set value properly on serverside, is now "..Money.Value)
end)

However when I print at the very end to make sure the server has the correct value, it has the old value from before this even started (based on information received from a datastore when the player joins)

So my question is: How can I push over the update to the Money.Value on the server side?

I've already searched both devform, the SH discord and this site it's self (i know, rare for someone to search right?) but I don't think any of those help for my particular issue.

Thanks in advance.

1 answer

Instead of changing the value Client-Side, try using a RemoteEvent to change the value for the player, therefore the server can read it.

1 comment

0

That's what I've done, I'm not quite sure what you mean otherwise. In the first codeblock I fire WithdrawFunction:FireServer(Money.Value), then script (a server script) fires on that event and sets the value

This discussion is preserved as it was. New answers, comments, and votes are closed.