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

How To Make Click Detector Add Coins when NPC died?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
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. darkeyescr 7 — 4y
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. darkeyescr 7 — 4y
0
Could you explain how it "respawns"? Ziffixture 6913 — 4y
0
I edited the post. darkeyescr 7 — 4y
View all comments (5 more)
0
Try the code I edited Ziffixture 6913 — 4y
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. darkeyescr 7 — 4y
0
it should be something like, canCollect: Connect () at the beginning, and after disconnect () darkeyescr 7 — 4y
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). Ziffixture 6913 — 4y
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! darkeyescr 7 — 4y
Ad

Answer this question