ServerScript won't update through the client

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    It’s a bit hard to explain but I hope the video below will explain to you. For some reason, the money didn’t save the previous value when buying an item through localscript. It just saves the previous money I had before I buy an item.

  2. What is the issue? Include screenshots / videos if possible!
    LocalScript:

local function toolChecker()
	local Tool = rep.Tools["Troll Burger"]:Clone()

	for _,v in pairs(player.Character:GetChildren()) do
		if v:IsA("Tool") then
			print("Didn't pick up the tool! Inventory full")
		end
	end

	for _,v in pairs(player.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			print("Didn't pick up the tool! Inventory full")
		end
	end
	
	if #player.Backpack:GetChildren() == 0 then
		Tool.Parent = player.Backpack
	end
end

clone.MouseButton1Click:Connect(function()
				if choice.Buy == true then
					local trollburger = 5
					local goodbyetext = "Comeback when you have enough money."
					if trollburger <= player.Data.Cash.Value then
						toolChecker()
						player.Data.Cash.Value = player.Data.Cash.Value - trollburger
						storedChoices[i] = nil
						nextDialog(info, choice)
					else
						storedChoices[i] = nil
						choiceDestroy()
						typeWrite("Looks like you don't have enough money to buy the epic Troll Burger.", dialoglabel)
						task.wait(1.1)
						typeWrite(goodbyetext, dialoglabel)
						task.wait(1)
						endDialog(info)
					end
				else
					storedChoices[i] = nil
					nextDialog(info, choice)
				end
			end)

ServerScript (Cash Collect)

local click = script.Parent.ClickDetector

click.MouseClick:Connect(function(plr)
	if plr then
		plr.Data.Cash.Value = plr.Data.Cash.Value + 1
		script.Parent.Parent.Parent.CashSound:Play()
		script.Parent.Parent:Destroy()
		wait(2)
		script:Destroy()
	end
end)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to add “if plr then” statement but that didn’t work at all.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

You have changed the Cash value in your LocalScript. You should not do that.

When a LocalScript modifies an object in the game, the change will be made on the client (player only), but will not replicate (send signal to change) to the server. For example, if a LocalScript creates a part, the part will only exist on that player’s workspace. The server will reject the change and not show it to the other players. This makes it possible to make changes in the workspace that only one player will see.

To fix this, you need to change the value on the server. Send a message to the server that the burger was bought and decrease the money value there. For that we have got RemoteEvent :3

Here, try this one:

-- Place this to where your decrease the value in your LocalScript
game:GetService("ReplicatedStorage").BuyBurger:FireServer()

And for the server:

-- You can place this in a new server Script in ServerScriptService.
local trollburgerPrice = 5

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BurgerEvent = ReplicatedStorage.BuyBurger

BurgerEvent.OnServerEvent:Connect(function(Player)
    player.Data.Cash.Value = player.Data.Cash.Value - trollburgerPrice
end)

The only thing that is left is create a RemoteEvent object in ReplicatedStorage and name it BuyBurger. :upside_down_face:

1 Like

Is there another way other than using RemoteEvents?

1 Like

Only if you place something like ClickDetector instead of a button, or ProximityPrompt. In my 4 years of scripting I always used RemoteEvents for UI → Server actions.

1 Like