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

Why this ontouch script don't work?

Hello everyone,I have problem with ontouch script. It should make supply chest disapear, but don't work ;s

Here is script:

local DebounceTime = 0.5

local Debounce = false
local Sup = script.Parent.Parent:WaitForChild("Supply")

local OnTouch = function(Part)
	if game.Players:GetPlayerFromCharacter(Part.Parent) and not Debounce then
		Debounce = true
		for _, child in pairs(Sup:GetChildren()) do
			if child:IsA("BasePart") or child:IsA("UnionOperation") then
				child.Transparency = 1
				child.CanCollide = false
			end
			
			end
			wait(DebounceTime)
		Debounce = false
		end
	end
script.Parent.Sup.Touched:connect(OnTouch)

I didn't saw any error output.

2 answers

✓ Accepted answer

The problem might be when you're calling the function. Since basically the script is reading line 20 as this:

script.Parent.script.Parent.Parent:WaitForChild("Supply")

Which doesn't look right does it? Maybe you should try getting rid of the Script.Parentbit in line 20, like this:

Sup.Touched:connect(OnTouch)

Hope this solved your problem! If you have anymore question then please feel free to post them down below!

EDITED:

local DebounceTime = 0.5

local Debounce = false
local Sup = script.Parent.Parent:WaitForChild("Supply")
local Model = script.Parent.Parent.Parent.SupplyChest
local OnTouch = function(Part)
    if game.Players:GetPlayerFromCharacter(Part.Parent) and not Debounce then
        Debounce = true
        for _, child in pairs(Model:GetChildren()) do
            if child:IsA("Part") or child:IsA("UnionOperation") then
                child.Transparency = 1
                child.CanCollide = false
            end

            end
            wait(DebounceTime)
        Debounce = false
        end
    end
Sup.Touched:connect(OnTouch)

-UserOnly16Characters

10 comments

0

Still don't work.

0

You script seems like it should work fine but maybe the cause of the problem is the Parenting. Can you take a picture of the Hierarchy?

0

If you mean explorer screenshoot... https://gyazo.com/195d9bf937fef7bf5c3d2b3989ba38c2

0

@DevKarolus1 Can you give us a better picture of your Hierarchy with the location of the script?

0

Ok i think this one will be good.. https://gyazo.com/08a4fc2b191389377d088c05178914bb

0

Do you want the script to activate when the Player touches the Part called Supply?!?

0

Yes.

0

Does it work now?

0

Thanks work perfectly ;)

0

No problem. As I suspected your parenting was a bit off etc.

If the code I gave you does not work then check the tree of the Touched event

Like does the script's parent have a child called "Sup" or?

local DebounceTime = 0.5

local Debounce = false
local Sup = script.Parent.Parent:WaitForChild("Supply")

script.Parent.Sup.Touched:connect(function(prt)
	if game.Players:FindFirstChild(prt.Parent.Name) and Debounce == false then
		Debounce = true
		for _, child in pairs(Sup:GetChildren()) do 
			if child:IsA("BasePart") or child:IsA("UnionOperation") then
				child.Transparency = 1
				child.CanCollide = false
			end
		end
		wait(DebounceTime)
		Debounce = false
	end
end)

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