Issue with the player revive system

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want it to revive the player and restore the eatcount after the player purchass the product
  2. What is the issue? Include screenshots / videos if possible!
    so I’m trying to make a death gui when you have 2 buttion one for respawn and 2 for revied I’m having issue withe the revival button where when you hit the revived button in do a test purchass it well respawn the player but not restore the eat count the player had before they died
  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    i try added a CharacterAdded:Wait() before it load the EatCount in that didn’t work

hers my client script

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local respawnButton = script.Parent.MainFrame:WaitForChild("respawn")
local deathGui = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")

local reviveButton = deathGui.MainFrame:WaitForChild("revive")

local REVIVE_PRODUCT_ID = 3392989558

local respawnEvent = ReplicatedStorage:WaitForChild("RespawnEvent")

deathGui.Enabled = false

local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	
	humanoid.Died:Connect(function()
		deathGui.Enabled = true
	end)
end

if player.Character then
	onCharacterAdded(player.Character)
end

player.CharacterAdded:Connect(onCharacterAdded)

respawnButton.MouseButton1Click:Connect(function()
	deathGui.Enabled = false
	respawnEvent:FireServer("respawn")
end)

reviveButton.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(player, REVIVE_PRODUCT_ID)
end)

and hers my server script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local MarketplaceService = game:GetService("MarketplaceService")

local REVIVE_PRODUCT_ID = 3392989558

local lastEatCount = {}

MarketplaceService.ProcessReceipt = function(receiptinfo)
	if receiptinfo.ProductId == REVIVE_PRODUCT_ID then
		local player = Players:GetPlayerByUserId(receiptinfo.PlayerId)
		if player then
			local savedEat = lastEatCount[player.UserId] or 0
			
			player:LoadCharacter()
			
			player.CharacterAdded:Wait()
			task.wait(0.2)
			
			local eatCount = player:FindFirstChild("EatCount")
			if eatCount then
				eatCount.Value = savedEat
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

local function setupCharacter(player, character)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		local eatCount = player:FindFirstChild("EatCount")
		if eatCount then
			lastEatCount[player.UserId] = eatCount.Value
		end
	end)
end

Players.CharacterAutoLoads = false

local respawnEvent = ReplicatedStorage:WaitForChild("RespawnEvent")

respawnEvent.OnServerEvent:Connect(function(player, action)
	if action == "respawn" then
		player:LoadCharacter()
	end
end)

Players.PlayerAdded:Connect(function(player)
	player:LoadCharacter()
	
	player.CharacterAdded:Connect(function(char)
		setupCharacter(player, char)
	end)
end)

Objects inside Players stay there until the player leaves. It isn’t necessary to restore it.

You are looking for eatCount inside the player, but you never created it in the first place:

local function setupCharacter(player, character)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		local eatCount = player:FindFirstChild("EatCount")
		if not eatCount then
			eatCount = Instance.new("NumberValue") -- guessing it's a number value
			eatCount.Parent = player
			eatCount.Name = "EatCount"
		end
		lastEatCount[player.UserId] = eatCount.Value
	end)
end

I already have a EatCount value made from the other script what my script doing is after the player respawn its making 2 EatCount values when i don’t think it suppose to so I think thats why its not working right

In that case I would share the other script.

1 Like

Then share the other script. But I already told you half the fix, you don’t need to restore the value:

hers the script that craete the invalue this is all of the script i have for the system

local character = script.Parent
local humanoidRoot = character:WaitForChild("HumanoidRootPart")
local morphGui = character:WaitForChild("Union"):WaitForChild("BillboardGui")
local textLabel = morphGui:WaitForChild("size")
local usernameLabel  = morphGui:WaitForChild("username")
local placeLabel = morphGui:WaitForChild("placemarks")
local humanoid = character:WaitForChild("Humanoid")

local player = game.Players:GetPlayerFromCharacter(character)

local eatSound = Instance.new("Sound")
eatSound.Name = "EatSound"
eatSound.SoundId = "rbxassetid://6823523283"
eatSound.Volume = 1
eatSound.Parent = humanoidRoot

local eatCount = Instance.new("IntValue")
eatCount.Name = "EatCount"
eatCount.Value = 0
eatCount.Parent = player


local function updatePlacements()
	local playersData = {}
	for _, plr in ipairs(game.Players:GetPlayers()) do
		if plr and plr:FindFirstChild("EatCount") then
			table.insert(playersData, {
				Player = plr,
				Count = plr.EatCount.Value
			})
		end
	end

	table.sort(playersData, function(a, b)
		return a.Count > b.Count
	end)

	for i, data in ipairs(playersData) do
		if data.Player.Character then
			local char = data.Player.Character
			local union = char:FindFirstChild("Union")
			if union and union:FindFirstChild("BillboardGui") then
				local billboard = union:FindFirstChild("BillboardGui")
				local placeText = billboard:FindFirstChild("placemarks")
				if placeText then
					placeText.Text = tostring(i)
				end
			end
		end
	end
end


local function updateGui()
	textLabel.Text = tostring(eatCount.Value)
	if usernameLabel and player then
		usernameLabel.Text = player.DisplayName
	end
	updatePlacements()
end

updateGui()

local function growMorph()
	local growAmount = 0.05
	local currentScale = character:GetScale()
	local newScale = currentScale * (1 + growAmount)

	local growDifference = (newScale - currentScale)

	character:ScaleTo(newScale)
	humanoidRoot.CFrame += Vector3.new(0, growDifference * 2, 0)
end

local function regenerateBall(originalBall)
	task.delay(3, function()
		if workspace:FindFirstChild("balls") then
			local newBall = Instance.new("Part")
			newBall.Name = originalBall.Name
			newBall.Shape = Enum.PartType.Ball
			newBall.Size = originalBall.Size
			newBall.Position = originalBall.Position
			newBall.Color = originalBall.Color
			newBall.Anchored = originalBall.Anchored
			newBall.CanCollide = originalBall.CanCollide
			newBall.Parent = workspace:WaitForChild("balls")
			
			local touched = false
			newBall.Touched:Connect(function(hit)
				if not touched and hit.Parent == character then
					touched = true
					eatCount.Value += 1
					updateGui()
					growMorph()
					
					if eatSound then
						eatSound:Play()
					end
					newBall:Destroy()
					regenerateBall(originalBall)
				end
			end)
		end
	end)
end

for _, ball in ipairs(workspace:WaitForChild("balls"):GetChildren()) do
	if ball:IsA("Part") then
		local touched = false
		ball.Touched:Connect(function(hit)
			if not touched and hit.Parent == character then
				touched = true
				eatCount.Value += 1
				updateGui()
				growMorph()
				
				if eatSound then
					eatSound:Play()
				end
				ball:Destroy()
				regenerateBall(ball)
			end
		end)
	end
end

humanoidRoot.Touched:Connect(function(hit)
	local otherChar = hit.Parent
	if otherChar and otherChar ~= character and otherChar:FindFirstChild("Humanoid") then
		local otherPlayer = game.Players:GetPlayerFromCharacter(otherChar)
		if otherPlayer and otherPlayer:FindFirstChild("EatCount") then
			local otherCount = otherPlayer.EatCount.Value
			if eatCount.Value > otherCount then
				eatCount.Value += otherCount
				updateGui()
				growMorph()
				if eatSound then eatSound:Play() end
				
				otherChar:FindFirstChild("Humanoid"):TakeDamage(1000)
			end
		end
	end
end)


eatCount.Changed:Connect(updatePlacements)

Looking at your code, I’m guessing this script is a server-sided script located in StarterCharacterScripts (judging by the local character = script.Parent).

If so then that’s the problem! Roblox duplicates this script to the player’s character every time it respawns. as the name says (StarterCharacterScripts). Because of this, this line of code and the entire script runs every time the player respawns:

To fix this, either change the script’s location to another place or replace that part of the code to this:

local eatCount = player:FindFirstChild("EatCount")
if not eatCount then
	eatCount = Instance.new("IntValue")
	eatCount.Name = "EatCount"
	eatCount.Value = 0
	eatCount.Parent = player
end

This is the reason why it’s not “restoring” the eat count. The value gets duplicated, so the script gets confused or just chooses the new value.

As I said earlier, you don’t need to restore any value, as any object inside a Player object will stay there until that player leaves the game, even if the player dies. So I would remove your value restoring logic or change it.

2 Likes

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