Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

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 Karl_RBX 11 — 7y
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. User#5423 17 — 7y
0
thanks :) Karl_RBX 11 — 7y
Ad

Answer this question