This could be done in a lot of different ways, but here is my take on it.
It might not be the most secure way of doing this, but I am not an expert and I just want to help out another fellow member of this community.
Inside the scripts I provide some explanations to how this system works.
In ReplicatedStorage I want you to put a RemoteEvent and name it "sellGems".
Because of Roblox's way of keeping your games more secure, we need this for the client's script to be able to contact the server script which will then change the leaderstats values.
In the ServerScriptService I want you to put a normal script, name it whatever you want but it doesn't really matter.
In your gui inside of your button I want you to put a localscript, name doesn't matter here either.
Now put this code in the local script.
local rs = game:GetService("ReplicatedStorage")
local sellGemsEvent = rs:WaitForChild("sellGems")
function onClick()
sellGemsEvent:FireServer()
end
script.Parent.MouseButton1Click:Connect(onClick)
Put this code in the server-side script.
local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("sellGems")
function sellGems(plr)
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value * 5
plr.leaderstats.Gems.Value = 0
end
event.OnServerEvent:Connect(sellGems)
Like I said, read the comments on the scripts where I explain what the code does.
Hopefully this works!
If you still get errors, check if the leaderstats work correctly and if your values are correctly named and capitalized.
4 comments
Is this a gui or a part
its a gui
Where is this gui? Is it a ScreenGui or a SurfaceGui? Please provide some more context on the situation, otherwise writing a solution is hard. And don't follow what Omer is saying, since you don't use a localscript to modify leaderstats. That won't be replicated amongst the server and other clients.
the gui is a screen gui, its a text button