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

Why is this not changing the value on touch?

I have it so that when you touch a part, it finds a value within the character and adds a preset amount that's found inside another value in the part.

debounce = false
script.Parent.Touched:connect(function(hit)
	if debounce == false
	then
		debounce = true
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent:FindFirstChild("money") then

			local money = hit.Parent:WaitForChild("money")
			local amount = script.Parent:WaitForChild("howmuchmoney").Value
			money.Value = money.Value + amount
			wait(.5)
			debounce = false
			
			end
		end
	end
end)

1 comment

0

Is this running in a server script and are there any errors in console?

1 answer

Make sure that "money" is stored in the character and not the player. Also, add print()s every time you are unsure if the code reaches that point to see what went wrong. If the code reaches past line 6 but not past line 7, we know that hit.Parent:FindFIrstChild("money") is returning nil, so there's something wrong with that. This is a trivial question for you to debug yourself, just remember to use print statements and find your mistakes.

debounce = false
script.Parent.Touched:connect(function(hit)
	if debounce == false then
	        debounce = true
		print("First statement pass")
		if hit.Parent:FindFirstChild("Humanoid") then
			print("Second statement pass")
	        	if hit.Parent:FindFirstChild("money") then
				print("Third statement pass")
				local money = hit.Parent:WaitForChild("money")
				local amount = script.Parent:WaitForChild("howmuchmoney").Value
				money.Value = money.Value + amount
				wait(.5)
				debounce = false
				print("If it has reached this point, then it's working.")
			end
		end
	end
end)

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