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)
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)