How can I disable a script when clicking on a part?

Hello! I’m trying to script a part(button) that when it is clicked, it will disable a script, preventing a player from the opposite team to die. Like an Owner door. I have spent a lot of time trying to find a solution, yet I cannot find one. I made the script print text to show the click registers. Yet, there is nothing in the output. I would appreciate any sort of help on this. Thank you. Script is below

function onClick(Click)
	local owner = script.Parent.Parent.Parent.Parent.Parent.Owner.Value
	local check = Click.Parent:FindFirstChild("Humanoid")
	if check ~= nil then
		local user = game.Players:GetPlayerFromCharacter(Click.Parent) 
		if owner == user.Name then
			script.Parent.KillScript.Disabled = true
			print("Click")
		else
			script.Parent.KillScript.Disabled = false
			print("Click")
		end
	end
end

I assume your using a ClickDetector so:

MouseClick event’s don’t return the Character so it won’t continue the code because of the

if check ~= nil then

The MouseClick Event returns the player.

The code below should work.

function onClick(Player)
	local owner = script.Parent.Parent.Parent.Parent.Parent.Owner.Value
	if owner == Player.Name then
		script.Parent.KillScript.Disabled = true
		print("Click")
	else
		script.Parent.KillScript.Disabled = false
		print("Click")
	end
end

I totally missed that part of code! I tried it out, and I still aren’t getting anything in the output.

Could you try inserting a print statement before

if owner == Player.Name then

And checking what it prints?

Yes. I found the problem. It had to do with recognizing where the click was coming from.
This is the final code

click = script.Parent.Button.ClickDetector
function onClick(Player)
	local owner = script.Parent.Parent.Parent.Parent.Owner.Value
	if owner == Player.Name then
		if script.Parent.KillScript.Disabled == false then
			script.Parent.KillScript.Disabled = true
			script.Parent.BrickColor = BrickColor.new("Really red")
			print("Click")
		else
			script.Parent.KillScript.Disabled = false
			script.Parent.BrickColor = BrickColor.new("Lime green")
			print("Click")
		end
	else
		
	end
end
click.MouseClick:connect(onClick)

Thanks a lot for your help!

1 Like
local parent = script.Parent
local button = parent.Button
local click = button.ClickDetector
local killScript = parent.KillScript

local function onClick(Player)
	local owner = script.Parent.Parent.Parent.Parent.Owner.Value --please find a way to make this reference cleaner
	if owner == Player.Name then
		parent.BrickColor = if killScript.Disabled then BrickColor.new("Lime green") else BrickColor.new("Really red")
		killScript.Disabled = not killScript.Disabled
	end
end

click.MouseClick:Connect(onClick)

Some cleaning up, don’t use “:connect()” as it’s the deprecated connection method, use “:Connect()” instead.

2 Likes