Weird logic in script

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!
    A rock that when you touch it, it changes your surroundings…

  2. What is the issue? Include screenshots / videos if possible!
    Script doesnt work, but no errors are present.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Added a loop, no.
    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 is an example Lua code block

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.

Memory leak, you’re creating a new connection on each loop without deleting the last.

It will never pass that if statement, because the :FindFirstChild() method will never pass true, or a boolean at all. remove == true (nothing else, just that) and it will work.

:FindFirstChild() returns an instance?, meaning an Instance or nil.

Try this code :

print("0")
local debounce = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and not(debounce) then
        script.OHNO:Play()
        debounce = true
        wait(5)
        debounce = false
        game.Workspace.Affected.Value = true
        print("1")
    end
end)

From what I understand, your while loop is useless because your .Touched event will always fire when something touches your part. @ClearlyVi is right, your code is creating many connections causing memory leak.

Thanks, now its fixed!​​​​​​​​​​​​​​​​​​​​​​​

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.