Timer not working

Hey Developers!
I am currently making a story game, and when I was making the DeathGui, I came across two problems!
Problem 1: For some reason, the timer instantely skips to 1! The whole point is, is that the player is supposed to have 10 seconds to buy the respawn products, if they don’t they get teleported back to the lobby. If they do, the timer stops. The timer should only start once the player dies. And for some reason, the products don’t prompt a purchase. For the prompt purchase, I get no errors, nor with the timer, apart from the Cannot Teleport In Roblox Studios

My Layout:

Screenshot_13

My Timer script:

local Player = game.Players.LocalPlayer
local TeleportService = game:GetService('TeleportService')

for i = 10, 1, -1 do
	script.Parent.Text = i
	if i == 1 then
		TeleportService:Teleport(4829096567, Player)
	end
end

My Prompt Purchase script

MPS = game:GetService('MarketplaceService')
Player = game:GetService('Players').LocalPlayer
Spawn1Id = 967744749
Spawn2Id = 967744795
Spawn3Id = 967744812
Spawn4Id = 967744826
Spawn5Id = 967745107
DeathAmount = Player:WaitForChild('Deaths').Value

script.Parent.MouseButton1Click:Connect(function()
	if DeathAmount == 1 then
		MPS:PromptProductPurchase(Player, Spawn1Id)
	elseif DeathAmount == 2 then
		MPS:PromptProductPurchase(Player, Spawn2Id)
	elseif DeathAmount == 3 then
		MPS:PromptProductPurchase(Player, Spawn3Id)
	elseif DeathAmount == 4 then
		MPS:PromptProductPurchase(Player, Spawn4Id)
	elseif DeathAmount >= 5 then
		MPS:PromptProductPurchase(Player, Spawn5Id)
	end
end)

And my show death screen script:

local Player = game.Players.LocalPlayer

Player.Character:WaitForChild('Humanoid').Died:Connect(function()
	wait(1)
	script.Parent.Frame.Visible = true
	script.Parent.Frame.Position = UDim2.new(0,0,-1,0)
    script.Parent.Frame:TweenPosition(UDim2.new(0,0,0,0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 1)
end)

Any help is appreciated!

1 Like

There’s no wait(1) in your for loop

Yes, but for some reason the script activates before the GUI is visible.
And the prompt purchase script doesn’t work.

Because you’re only getting the value one single time, which is once the script runs. Make this line look like that DeathAmount = Player:WaitForChild('Deaths') and then check for it’s value using for example if DeathAmount.Value == 1 then

Actually, I just realised the problem. When the player dies, I forgot to add to the players death!
And finally, why is the countdown script activating when the UI Isn’t visible, How can I make it so that it doens’t activate until the player dies?

I don’t see anything that’d be preventing it from running

It does run!
I don’t think you understand, I’m saying that it should only run when the player dies, not when they join.

Oh well in that case you should keep the script disabled and enable it once .CharacterAdded event fires

If you want the timer to start whenever the character spawns, wrap your script in this:

Player.CharacterAdded:Connect(function()

end)

If you want the timer to start whenever a player dies wrap your script in this:

Player.CharacterAdded:Connect(function()
Player.Character.Humanoid.Died:Connect(function()


end)
end)

Ok, so for the timer, you don’t even have a wait() in it. After changing the text, but before teleporting the player, add a wait(1) line.

This will also run immediately when the player joins, so you should do this:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

humanoid.Died:Wait()

--Rest of code

As for the prompting purchase thing, you’re prompting different products based on the value of something. However, the variable you assigned it to is always 0, as you declare the variable before anything, so it becomes 0, therefore, nothing prompts at all. Move the variable inside of the MouseButton1Click event like this:

script.Parent.MouseButton1Click:Connect(function()
    local DeathAmount = Player:WaitForChild('Deaths').Value --I put this in the event
    --Rest of code
end)
1 Like