Weird sell and mine behaviour

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)

show me a leader stats script, maybe you have coal value set as 1 by default when u defining coal variable

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.

The issue is that the server doesn’t know about the change you made to the Cash and Coal values, because you changed them on the client.

We can use something called Remote Events to communicate between the Server and Client.

Learn about Remote Events and the differences between the server and the client. Afterwards, you’ll be able to solve this issue pretty easily.

    local Coal = Instance.new("IntValue")
	Coal.Name = "Coal"
	Coal.Parent = PlayerJoined.leaderstats
	Coal.Value = 0

So i assume i can do that just by switching the script to a server script?
And replace local player?

Alright Ill make sure to check that out

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.