Why the coins are going back to normal?


here’s the script i use
first part

second part

2 Likes

Cause you are adjusting the money value on the client. Stuffs on the client won’t be replicated to the server. So you have to use RemoteEvent or RemoteFunction.

2 Likes

local buyButton = script.Parent

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local remoteEvent = ReplicatedStorage:WaitForChild(“RemoteEvent”)

local function buy()
local player = game.Players.LocalPlayer
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - script.Parent.Parent.Parent.ScrollingFrame.Cost.Value
end

buyButton.MouseButton1Click:Connect(buy)

i did this but it’s not working

1 Like

You didn’t use the remote event for anything. Do a FireServer() when the button is clicked. And then in the server, you minus the money value.

-- Local script
button.MouseButton1Click:Connct(function()
    remoteEvent:FireServer("the value you will minus")
end)

-- Server script
remoteEvent.OnServerEvent:Connect(function(player,value)
    -- Minus the player's money
end)

I recommend you read about the Client-Server Model.

1 Like

like this?

Local Script =

local button = script.Parent

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

button.MouseButton1Click:Connect(function()
    remoteEvent:FireServer("Coins")
end)

Server Script =
local player = game.Players.LocalPlayer

local value = player.leaderstats.Coins.Value
local price = game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player,value)

- value

end)

1 Like

Hey there, welcome to the DevForums. Please be sure to read category guidelines before making posts here. The Code Review category is for getting tips for improving code that already works. If you need help on a problem, use the Scripting Support category. I’ve recategorised the thread for you.

1 Like

You can not define local player in a serverscript. The first paramter to OnServerEvent is the player whom it was fired for. The second one that you have put in is “value” but you can not use that as you did not pass it along in your local script.

Local script

local button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

button.MouseButton1Click:Connect(function()
    remoteEvent:FireServer()
end)

Server script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player) --Any parameter after player is a tuple, which means you can give it any name, but it must be passed on from the :FireServer()
Value = player.leaderstats.Coins.Value

end)

sorry if i am asking too much but i’m new at scripting and i don’t know that much and sorry for wasting your time guys :disappointed_relieved:

here’s the server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player, Coins:FireServer(remoteEvent))

- value

end)

You should not have a FireServer() as one of your parameters. Sorry if I confused you with my comment. I meant that you pass arguments from the local script to the server script in the FireServer parenthesis. Just look at what I did in the local script, and then just give it a name in the server script, such as coins

:thinking:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player, Coins)

remoteEvent:FireServer(game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value)

end)

I have actually edited my original answer, as it was getting the values of coins on the local side, which could become a problem if an exploiter was to change their values of coins.

With this edit, you do not need to pass any arguments on to the server, and you can just do :FireServer() with nothing in the parenthesis.

That also means that all you need is this for the server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player)
     Value = player.leaderstats.Coins.Value
end)

and this for the local script.

local button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

button.MouseButton1Click:Connect(function()
    remoteEvent:FireServer()
end)

And with your script that you have attempted, you can not fire the server inside of an onserverevent function. Oh and you never want to get something from the startergui, as everything inside of there gets cloned to the player when they join, and changes are made to their playergui only, not the startergui.

1 Like

should i change something? or i just copy and paste?

1 Like

All you need to do is copy and paste, but you need to add in what you want to happen to the coins. Like subtracting them from the value

1 Like

do i need to change both of them or only one of them?

1 Like

You will only need to do something like Value = Value - 500. Something along those lines, since I’m not exactly sure the amount you want to subtract. And this goes in the onserverevent function in the server script

1 Like

here’s what i want to do

value = game.Players.LocalPlayer.leaderstats.Coins.Value - game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value

1 Like

Ah okay I see.

remoteEvent.OnServerEvent:Connect(function(player)
     Value = player.leaderstats.Coins.Value
end)

This part in the server script already defines the value of coins using the player parameter that the onserverevent function provides. You will then want to puit this inside of the onserverevent function.

Value = Value - game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value

Also, just a note. In this situation addressing the startergui is fine, but if you even want to make changes to a GUI you need to use PlayerGui.

1 Like

ok tell me if i did something wrong!
local script
script3help
server script
script4help

Move this line Value = Value - game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value into here so it looks like

remoteEvent.OnServerEvent:Connect(function(player)
     Value = player.leaderstats.Coins.Value
     Value = Value - game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value
end)

And remove it entirely from the local script

should it look like this?
local script
script5help
server script
script6help

1 Like