Local script doesnt open/close door when pressing button

Hello! Today I tried to code a simple door opening script that makes the door almost invisible and turns off the collision, but it didn’t work. I really tried everything, but nothing seemed to fix it. :slightly_frowning_face: The code I wrote is a localscript in a part called “Button”.

local button = script.Parent

button.Touched:Connect(function()
	workspace.Door.CanCollide =  false
	workspace.Door.Transparency = 0.9 -- make the door almost invisible
	wait(2)
	workspace.Door.CanCollide = true
	workspace.Door.Transparency = 0.1 -- make the door almost fully visible
end)

( also it doesn’t show a error in the output window. )

1 Like

are you coding for mobile?
if you are coding for pc you have to use

button.MouseButton1Click:Connect(function()
your code
end)

its not a GUI button, it’s a part called “Button”

local Part = script.Parent 

Part.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild(“Humanoid”) then
workspace.Door.CanCollide = false
	workspace.Door.Transparency = 0.9
	task.wait(2)
	workspace.Door.CanCollide = true
	workspace.Door.Transparency = 0.1
end
end)
2 Likes

Local scripts do not work in workspace. Try using a server script or move the script to starter player and change the part variable accordingly if you only want the player who touched the part to see it. I had that error before too, if you added a print to the start of the script it would not print because the local script will only run in starter player or starter character. I hope this helps.

It is true that you can’t have local scripts in the workspace
but if you wish to keep the change Client-Side you should put the script into
(game.StarterPlayer.StarterPlayerScripts)

–Extra stuff
I recommend using workspace:WaitForChild(“Part name”)
and maybe a small cooldown.

1 Like

A LocalScript does not load when put into a Part nor something not related to the player itself, which makes sense because you want the code to be replicated only to the player. A solution is to put it either in StarterCharacterScripts or StarterPlayerScripts if you need it to be a LocalScript to Replicate only to the client. Or you could simply make a normal script, but it would replicate to everyone.

1 Like

use a server script rather than a local script

I believe you need to define the workspace as it’s own service, because it is a local script. If you are running this script in the workspace, you can’t do that. Instead, you’d have to change a normal script’s RunContext to Client. With your script in mind, this should be a viable solution, albeit with some edits.

Edit: Made some changes to the script to only have it run when a character touches it.

local ws = game:GetService("Workspace")
local button = script.Parent


button.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then --only run script when a character touches the door
		ws.Door.CanCollide =  false
		ws.Transparency = 0.9 -- make the door almost invisible
		wait(2)
		ws.Door.CanCollide = true
		ws.Door.Transparency = 0.1 -- make the door almost fully visible
	end
end)

If you have any problems running this script, let me know.

Best regards,

poker_man29

1 Like

.Touched is only used when anything collides with the part. If you want to make a button, you need to put a ClickDetector for it and use the .MouseClick function instead.

1 Like

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