IntValue adds way too many times

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to add to an intvalue only once, not twice or more

  2. What is the issue? Include screenshots / videos if possible!
    whenever i touch the coin, it adds to my coin value more than once

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i’ve tried adding debounces and disabling the CanTouch, and looked on the devforums but found nothing that could help

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

this script is supposed to add 1 to an intvalue whenever a coin is touched, then destroy the coin after. for some reason it adds to the intvalue way too many times

local debounce = true


coin2.PrimaryPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == true then
			debounce = false
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local money = player:WaitForChild("Money")
				local coined = money:FindFirstChild(coin)
				coined.Value += 1
				coin2:Destroy()
			end
		end
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Have you tried using :Once instead of :Connect ?

local debounce = true

coin2.PrimaryPart.Touched:Once(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == true then
			debounce = false
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local money = player:WaitForChild("Money")
				local coined = money:FindFirstChild(coin)
				coined.Value += 1
				coin2:Destroy()
			end
		end
	end
end)

yes, but it still adds to the intvalue twice.

Are you sure there is no 2 models stuck to eachother or smth?
and try this:

local debounce = true

coin2.PrimaryPart.Touched:Once(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == true then
			debounce = false
			coin2.PrimaryPart.CanTouch = false
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local money = player:WaitForChild("Money")
				local coined = money:FindFirstChild(coin)
				coined.Value += 1
				coin2:Destroy()
			end
		end
	end
end)

I don’t recommend using :Once as if it was not touched by a character model with a humanoid as a child, it wouldn’t be able to destroy the coin model, as you can only use :Once once, leaving you with another issue.

i think he can also check if the touching model has a player too using GetPlayerFromCharacter

nah it doesn’t work, even if the CanTouch property is false. i’ve tried using this script on different models and they have the same issue aswell.

Set the PrimaryPart to nil, inside the debounce.

You should debug your code.
Here is an example:

local debounce = true

coin2.PrimaryPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == true then
			print("Touched")
			debounce = false
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local money = player:WaitForChild("Money")
				local coined = money:FindFirstChild(coin)
				coined.Value += 1
				coin2:Destroy()
			end
		end
	end
end)

If it prints Touched more than once, that means something is wrong, if not then the issue is something else.

yeah, it definitely has something to do with the intvalue or something since it only prints Touched once.

Check if ur duplicating it somewhere.

Do you have the same function located somewhere else in your game?

no, i don’t, this is the only place it’s at.

Would you be able to tell me the current value of the intvalue and what you intend it to be after you have touched the coin?

This is helpful for example as if the current value of the intvalue is 1, and whenever you touched the part you intend it to be 1, as you thought the current value of the intvalue was 0.
However when you have tested it, the intvalue was 2, causing you to think that it added 2.

the value is 0 and not 2, it always adds 2 or 1 to the intvalue whenever the coin is touched.

Would you be able to send the code, that parents the intvalue to the Player?

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local money = Instance.new("Folder")
	money.Name = "Money"
	money.Parent = player

	local pound = Instance.new("IntValue")
	pound.Name = "Pound"
	pound.Value = 0
	pound.Parent = money
end

Players.PlayerAdded:Connect(leaderboardSetup)

Their really isn’t anything that I could do, but you could
Open up a new baseplate and add only the coin and the scripts inside.

have u tried changing the intvalue to a numbervalue and see if that helps?

it only gives twice whenever the coin is cloned from a script.