Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

ClickDetector does not read updated NumValue from player tool?

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

0

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

1 answer

So the reason it was doing this had nothing to do with the ClickDetector; all of that code works exactly as it is intended.

The problem is that I was subtracting the values inside of a seperate LocalScript. Whenever I instead used a RemoteEvent to carry out the actual subtraction inside of a server Script it works exactly like it should.

I spent literally 5 hours trying to fix this problem and it was my own stupid use of LocalScripts, and malicious understanding of the Client-Server model.

This discussion is preserved as it was. New answers, comments, and votes are closed.