Problem with script detecting when player touches a part

I’m trying to create a script to detect when a player touches a part, then changes another parts properties such as Cancollide.

My script isnt working for some reason and, I’m trying to find somebody who understands why, I’ve tried to make a new script with no success.

Here is the script

local p = game.Workspace.Part

local door = game.Workspace.Part2

local function touch(Part)
	local partParent = game
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		door.CanCollide = false
	end
end
1 Like

Your script also isn’t working cause you’re defining the partParent as game and not the parameter the Touched event gives you, why though?

Try this:

local p = game.Workspace.Part

local door = game.Workspace.Part2

local function touch(Part)
	if Part.Parent:FindFirstChild("Humanoid") then
		door.CanCollide = false
	end
end

p.Touched:Connect(touch)
2 Likes

local p = game.Workspace.Part

local door = game.Workspace.Part2

local function touch(Part)
if Part.Parent:FindFirstChild(“Humanoid”) then
door.CanCollide = false
end
end

print(p.Touched)

Your script won’t work cause you’re not connecting the Touched event with the function, plus I don’t think printing the event will even work

That should only be if you need to debug the script if necessary

oh ok. I am just trying to help and not the best scripter ever

I don’t know why but nothing at all happens when I touch the part, not any error messages or anything.

Does it matter if its a localscript or script? Maybe its in the wrong location? its currently located at ServerScriptService.

It has to be a normal script for it to work.

Nothing changed by doing that, I changed it from a localscript to a normal script.

I dont think there is a problem with the script…

Well do the parts exist at all?

Yes they do, they are in workspace and are normal parts, one is yellow, one is black.

Okay debugging script then

print("Script online")
local p = game.Workspace.Part
local door = game.Workspace.Part2

local function touch(Part)
    print("Part touched")
	if Part.Parent:FindFirstChild("Humanoid") then
        print("Door colission changed")
		door.CanCollide = false
	end
end

p.Touched:Connect(touch)

So the script works ingame but not in studio??

Why not in Studio though? Can you check your Output? :thinking: It should be a ServerScript, either in ServerScriptService or workspace

Ok so when I playtest in studio it works but not when the part is touched.

Nevermind, Its working fully now!

1 Like

Oh nice! Glad you managed to fix it! All it took was 15 minutes of pain aha

1 Like