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

How To Make Click Detector Add Coins when NPC died?

How To Make Click Detector Add Coins when NPC died?

To put a condition to collect the point, only when the character is simply dead. how can i do this using the script below? the model is Judoon. I tried something, but it does not come out for me, because he unlocks the button permanently add coins, does not resume the condition every time.

so if the character Judoon is dead, will collect 10 points. if the character is alive, it will not collect the 10 points.


local CanClick = script.Parent:WaitForChild("CanClick") local ClickDetector = script.Parent modelname = "Judoon" -- Model name model = game.Workspace:FindFirstChild(modelname) local function onClick(playerClicked) if (playerClicked and CanClick.Value ~= false and model:FindFirstChild("Humanoid") ~= nil) then CanClick.Value = false local Leaderstats = playerClicked:FindFirstChild("leaderstats") if (Leaderstats and Leaderstats.Coins) then Leaderstats.Coins.Value = (Leaderstats.Coins.Value + 10) end wait(1) CanClick.Value = true end end ClickDetector.MouseClick:Connect(onClick)


1 answer

✓ Accepted answer

You can use the .Died event of Humanoid to encapsulate the onClick() function. This way, you cannot actively listen for the .MouseClick signal until the avatar dies.

local canClick = script.Parent:WaitForChild("CanClick")

local clickDetector = script.Parent

local npcModel = script:FindFirstAncestor("Judoon")
local humanoid = npcModel:FindFirstChildOfClass("Humanoid")

local function onClick(playerClicked)
    if (playerClicked and canClick.Value ~= false) then
        canClick.Value = false
        local leaderstats = playerClicked:FindFirstChild("leaderstats")
        if (leaderstats and leaderstats .Coins) then
            leaderstats.Coins.Value = (leaderstats.Coins.Value + 10)
        end
        wait(1)
        canClick.Value = true
    end
end

humanoid.Died:Connect(function()
    local canCollect;
    canCollect = clickDetector.MouseClick:Connect(onClick)
    repeat wait() until humanoid:GetState() ~= Enum.HumanoidStateType.Dead
    canCollect:Disconnect()
end)

9 comments

0

unlock the permanent button. it does not resume the condition every time the character repawns. after the Judoon is killed once, they can collect the points, before they are killed, they cannot collect them. it is ok so far, the problem is that after that they can collect the points and do not resume their condition again.

0

it is the same problem that I encountered up top, it works the first time, when the character repawns, it simply collects the points continuously.

0

Could you explain how it "respawns"?

0

I edited the post.

0

Try the code I edited

0

now it works, but after it no longer collects the point, I can collect only once. when the character returns, it no longer collects the point when you press the button again.

0

it should be something like, canCollect: Connect () at the beginning, and after disconnect ()

0

Here: I’ll update the code to handle this. Just ensure you put the script within the PrimaryPart (Or whichever part of the Judoon wish you to be allowed to be clicked).

0

I managed to do what I wanted very simple, modifying simple script clicker collect and introduced it into the respawn. all I wanted was to collect the point when I clicked the button, but only after the character was dead. thank you very much for your help!

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