How do i detect when a player dies multiple times?

i want to make a feature where if one person dies three times in a roe on a specific stage it prompts a developer product

how would i do this

1 Like

This can either be done in the Client or the Server. However, it may be best to use the Client for this case.

Just check if the Player’s Humanoid is dead (you can use the Humanoid.Died event) and add the number of deaths to a variable. If it reaches 3, you send them the prompt. If the stage changes, you can reset it to 0.

Here is a brief example:

-- This script is meant to be placed in StarterPlayerScripts
local MPS = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
local deaths = 0

plr.CharacterAdded:Connect(function(char)
    local hum = char:WaitForChild("Humanoid")
    hum.Died:Connect(function()
        deaths += 1
        if deaths == 3 then
            MPS:PromptProductPurchase(plr, productID) -- replace productID with the ID
        end
    end)
end)

-- This is assuming that the stage is something such as an IntValue, so it can detect whenever the stage changes.
Stage.Changed:Connect(function()
    deaths = 0
end)
4 Likes

can i just detect if the leaderstat value changes and set the deaths to 0 that way?

1 Like

If that’s what you’re trying to accomplish, then you can likely achieve that result by doing something similar to the following:

leaderstat:GetPropertyChangedSignal("Value"):Connect(function()
  deaths = 0
end)

Let me know if this doesn’t work, or I misunderstood what you were asking for.

1 Like

I would create an int value in the player somewhere (not leaderstats, because you probably don’t want it to show up), and every time the player dies, check that value to see if it’s 3 (or, alternatively, divisible by 3 so it shows up every 3 deaths), and if it is 3 then prompt the product purchase. When the player triggers a checkpoint, set the value back to 0.

put this localscript in startercharacterscripts

local hum = script.Parent:WaitForChild("Humanoid")
local productid = PUTYOURPRODUCTIDHERE

if not hum:GetAttributes()["Deaths"] then
   hum:SetAttribute("Deaths", 0)
end
local deaths = hum:GetAttribute("Deaths")
if deaths == 3 then
   hum:SetAttribute("Deaths")
   game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productid)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.