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

Why does this click function not work?

I'm trying to make a door that when clicked, makes whoever clicked it the owner of the door as long as nobody owns it. If someone does own it, you can't open it unless you own it. My main issue is the "clickey" function - it doesn't work, for some reason. It's connected to a ClickDetector. There are no errors, but the DoorOwner variable doesn't get set.

owner = script.Parent.Parent.DoorOwner

function clickey(playerwhoclicked)
if owner.Value == "nobody" then
	owner.Value = playerwhoclicked.Name
	script.Parent.Parent.Back.TextLabel.Text = "Owned by" .. playerwhoclicked.Name
	script.Parent.Parent.Front.TextLabel.Text = "Owned by" .. playerwhoclicked.Name
elseif owner.Value == playerwhoclicked.Name then
	print("worky")
end
end

--The loop below is perfectly fine. The function above is what I'm having trouble with.

while true do
	wait(2)
	if owner.Value ~= "nobody" then
	player = game.Players:FindFirstChild(owner.Value, true)
		if player == nil then
		owner.Value = "nobody"
		end
	end
end

script.Parent.MouseClick:connect(clickey)

3 comments

0

Is the Parent on line 25 a ClickDetector? If not, then that may be your problem, as you can only use the event 'MouseClick' on a ClickDetector asset. :)

0

It's because you have a while loops which continually runs before reaching the MouseClick event connection line. What you could do is use the PlayerRemoving event and find if the owner (I am assuming you're making a tycoon) has left the server.

0

^

1 answer

✓ Accepted answer

##Problem

Your connection line is after a continual loop that does not stop. Therefore the event never gets established.


##Solution

Use the PlayerRemoving event to detect if the player is leaving the server.


##Final Script

owner = script.Parent.Parent.DoorOwner

function clickey(playerwhoclicked)
    if owner.Value == "nobody" then
        owner.Value = playerwhoclicked.Name
        script.Parent.Parent.Back.TextLabel.Text = "Owned by" .. playerwhoclicked.Name
        script.Parent.Parent.Front.TextLabel.Text = "Owned by" .. playerwhoclicked.Name
    elseif owner.Value == playerwhoclicked.Name then
        print("worky")
    end
end

--The loop below is perfectly fine. The function above is what I'm having trouble with.

function PlayerRemoving(Player) --Define the new function.
    if owner.Value == Player.Name then --If the player that is leaving the game has the same name as the owner then do something.
        owner.Value = "nobody" --The script will now change the owner of the place to nobody.
    end
end

game.Players.PlayerRemoving:connect(PlayerRemoving)
script.Parent.MouseClick:connect(clickey)

######Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to leave a comment below.

1 comment

0

It worked, thanks! There was an rror on line 19, but that was pretty easy to fix.

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