Im trying to make something where you can sell ore for money however when you sell lets say 5 ore and mine again you get 6???
I dont even know how i can solve this or if its a studio bug
heres the mining script
local Coal = script.Parent.Parent
local Click = script.Parent
local Health = Coal.HealthPoints
local HealthDisplay = Coal.BillboardGui.TextLabel
Health.Value = 10
HealthDisplay.Text = 10
HealthDisplay.Text = " "..Health.Value
Click.MouseClick:Connect(function(Clicked)
Clicked.leaderstats.Coal.Value += 1
Health.Value -= 1
HealthDisplay.Text -= 1
if Health.Value == 0 then
Coal.Parent = game.ReplicatedStorage
wait(30)
Coal.Parent = game.Workspace.MapFolder
end
end)
now heres the sell script
local Button = script.Parent
Button.MouseButton1Click:Connect(function(Clicked)
game.Players.LocalPlayer.leaderstats.Cash.Value += 1
game.Players.LocalPlayer.leaderstats.Coal.Value -= 1
if game.Players.LocalPlayer.leaderstats.Coal.Value == 0 then
Button.Visible = false
else
Button.Visible = true
end
end)
You’re changing the coal value on the client. The server cannot see this change, so you would have to change the coal value on the server. It’s really a bad idea to change important values’s from the client.
You can fire a remote event once the button has been clicked. And in a server script, you would listen for that remote event so that when it’s fired, you can check the player’s coal value and increment their cash value and decrement their coal value. It can be accomplished like so:
local Button = script.Parent
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent -- Your remote event should be in replicated storage.
Button.MouseButton1Click:Connect(function(Clicked)
remoteEvent:FireServer()
if game.Players.LocalPlayer.leaderstats.Coal.Value == 0 then
Button.Visible = false
else
Button.Visible = true
end
end)
And in a server script, you should listen for the remote like so:
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent -- Your remote event should be in replicated storage.
remoteEvent.OnServerEvent:Connect(function(player) -- This is a check for when 'RemoteEvent' is fired.
--This will run after the remote event is fired.
local leaderstats = player:WaitForChild("leaderstats")
if leaderstats then
if leaderstats.Coal.Value >= 1 then -- This is to check if the 'Coal' value is above 1.
leaderstats.Coal.Value -= 1
leaderstats.Cash.Value += 1
end
end
end)
Please make sure to read through the comments and examine the code so that you can understand what each line does.