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

Making a tycoon only owner door working etc?

Imagine you have a working owner only door with a button and lasers right? but you wanna stop other players who dont own the tycoon from opening the door with the button when new people are tying to build there tycoon.

How would this work? or How would you write this out in code to do those types of checks?

local Enabled = true
local Lasers = script.Parent.Lasers
local Button = script.Parent.Button
local OwnerValue = script.Parent.Parent.Parent.Values.OwnerValue

Button.ClickDetector.MouseClick:Connect(function()
	if Enabled == true then
		Enabled = false
		Lasers.Transparency = 0.8
		Lasers.CanCollide = false
	else
		Lasers.Transparency = 0
		Lasers.CanCollide = true
		Enabled = true
	end
end)

Lasers.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if OwnerValue.Value ~= Player then
			if Enabled == true then
				Hit.Parent:FindFirstChild("Humanoid").Health = 0
			else
				return end
		end
	end
end)

this is my code so far

1 answer

✓ Accepted answer

To answer your question, the ClickDetector.MouseClick event returns the player who clicked. You can simply use the code you wrote before to check if the player is equal to the ownerValue.

Personally, I'd recommend writing a function to make your code appear cleaner, as well as creating a variable for the humanoid, if you're going to use it again. Minor details, but not very important.

local Enabled = true
local Lasers = script.Parent.Lasers
local Button = script.Parent.Button
local OwnerValue = script.Parent.Parent.Parent.Values.OwnerValue


local function isOwner(player)
	if player == OwnerValue.Value then return true end
	return false
end

Button.ClickDetector.MouseClick:Connect(function(player)
	if isOwner(player) then
		if Enabled == true then
			Enabled = false
			Lasers.Transparency = 0.8
			Lasers.CanCollide = false
		else
			Lasers.Transparency = 0
			Lasers.CanCollide = true
			Enabled = true
		end
	end
end)

Lasers.Touched:Connect(function(Hit)
	local humanoid = Hit.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(Hit.Parent)

	if humanoid and player then
		if isOwner(player) == false  and Enabled then
			humanoid.Health = 0
		end
	end
end)

https://developer.roblox.com/en-us/api-reference/event/ClickDetector/MouseClick

1 comment

0

Wow, thank you so much that really helps out a lot to know what i'm doing work and what im not. Again Amazing job and thank you for the help!

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