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

Why wont this click detector script work?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

Hi I was making this script and I was wonder why this didn't work. Also it didn't have any output errors and it is in a local script. Can you help me with this script problem?

function OnClick()
local plr = game.Players.LocalPlayer
if plr:FindFirstChild("leaderstats") ~= nil then
                local clicks = plr:FindFirstChild("leaderstats"):FindFirstChild("Cow Clicks")
            if (clicks ~= nil) then
                clicks.Value = clicks.Value + 1
                script.Parent.Sound:Play()
            end
else
    print("leaderstats not found")
end

end

script.Parent.ClickDetector.MouseClick:connect(OnClick)

1 answer

Log in to vote
1
Answered by
funyun 958 Moderation Voter
8 years ago

You don't need a LocalScript for this, neither should you use one. The ClickDetector's MouseClick event passes the player that clicks as an argument to its connected function

function OnClick(theguythatclicked)
    --Get that tabbing under control.
    --One function, one conditional statement,
    --one loop, etc. gets one tab.
    --You should let the IDE do it for
    --you for the most part.

    local clicks = theguythatclicked:FindFirstChild("leaderstats"):FindFirstChild("Cow Clicks")

    if clicks then
        clicks.Value = clicks.Value + 1
        script.Parent.Sound:Play()
    end
end

script.Parent.ClickDetector.MouseClick:connect(OnClick) --Event passing player to function
0
didn't work ;( any ideas? docrobloxman52 407 — 8y
0
Did you get rid of the LocalScript and put it in a server script? funyun 958 — 8y
Ad

Answer this question