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

Can someone show me what i have done wrong in this click detecting script?

Knowing me there is an error in here, but i just dont know where its not working, this code is meant to make a part go invisible after 10 clicks and it does it on the first click, help will be much appreciated :) thanks!

function onClick()
script.Parent.Value.Value = script.Parent.Value.Value + 1
 if script.Parent.Value.Value ~= 10 then
	script.Parent.Transparency = 1
else
end

end


script.Parent.ClickDetector.MouseClick:connect(onClick)

1 answer

✓ Accepted answer

It is doing what you told it to do check the operator

You are using ~= so lets say we had the starting value of then we add 1. 1 ~= 10 which is true so the code will run and make the part go invisible.

We could use:-

-- this will run when the value is greater than 9
 if script.Parent.Value.Value > 9 then

-- this will run when the value is greater than or equal to 10
 if script.Parent.Value.Value >= 10 then

--- a lot of others  can be used as well 

Other issues:- We do not need to access script.Parent.Value multiple times so we should make a variable, this also helps simplify the code.

local par = script.Parent -- we create a variable to access the parent, we then use this 
local valObj = par.Value -- this is the object you should re-name it as value is confusing, also look into using "WaitForChild()"

print('variabels added')

function onClick()
	print('ran')
	valObj .Value = valObj .Value + 1

	 if valObj .Value == 10 then -- only run at 10
		print('num is 10')
	    par.Transparency = 1
	else
		print('num is not 10')
	end
end

par .ClickDetector.MouseClick:connect(onClick)

I hope this helps, please comment if you do not understand how /why this code works.

WaitForChild

3 comments

0

Even with yours it seems to not work, i can't seem to figure it out, the click value keeps going over 10 and doesn't go invisible

0

The click will go over 10 as we have not set any limits e.g. stop adding once we have 10, I will update the code to add a print() in this should show how far the code runs.

0

thanks :)

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