Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this click function not work?

Asked by
udoxas 75
8 years ago

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)
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. :) TheeDeathCaster 2368 — 8y
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. M39a9am3R 3210 — 8y
0
^ User#11440 120 — 8y

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

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.
0
It worked, thanks! There was an rror on line 19, but that was pretty easy to fix. udoxas 75 — 8y
Ad

Answer this question