here’s the script i use
first part
second part
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.
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
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.
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)
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.
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
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
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.
should i change something? or i just copy and paste?
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
do i need to change both of them or only one of them?
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
here’s what i want to do
value = game.Players.LocalPlayer.leaderstats.Coins.Value - game.StarterGui.Shop.Frame.ScrollingFrame.Cost.Value
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.
ok tell me if i did something wrong!
local script
server script
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
server script