The following script is placed inside of parts I have randomly placed on the baseplate (floor in this case) of the map. When it's clicked, it deletes the part, and replaces it with a normal floor cell.
local part = script.Parent
local rStorage = game:GetService("ReplicatedStorage")
if not part:FindFirstChild("ClickDetector") then
cd = Instance.new("ClickDetector")
cd.Parent = part
else
cd = part.ClickDetector
end
cd.MouseClick:Connect(function(player)
local base = game.ReplicatedStorage.Baseplate:Clone()
local oldPos = part.Position
base.Parent = game.Workspace.Floor
base.Position = oldPos
part:Destroy()
if not player.Backpack:FindFirstChild("MinedOre") then
if not player.Character:FindFirstChild("MinedOre") then
local Ore = rStorage.Tools.MinedOre:Clone()
Ore.Parent = player.Backpack
Ore.Value.Value = 1
else
local Ore = player.Backpack.MinedOre.Value
Ore.Value += 25
end
else
local Ore = player.Backpack.MinedOre.Value
Ore.Value += 25
end
end)
The problem I'm having is that it's supposed to give the player ore, which it does properly, except that whenever the player gains ore and then subsequently loses some, the script continues to add ore as if it had never been depleted.
I.e. I gain 50 ore, craft an item that needs 25, and then when I click on the part, it thinks I still have 50 and sets the value to 75.
I think it has something to do with the code only being run when the object is clicked, but all of my attempts to force it to check for the updated value have also failed.

1 comment
please indent properly. i cannot understand the code if it is not indented