RemoteEvent won't fire again after died

NOTE: This was 90 line code so i deleted useless parts so don’t worry about end and parentheses

I have theese codes. Everything works fine when i click E to part, game starts. But when player died and click E again to play, it won’t start again. I hope someone will understand and help me. Thanks.

LocalScript in StarterCharacterScripts:

local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

RS:WaitForChild("StartGame").OnClientEvent:Connect(function()

	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	
	if true then
--codes
		RS:WaitForChild("FitChar").OnClientEvent:Connect(function(some,things)
			task.wait(3)
			-- codes
			local function gameLoop()
				while task.wait(1) do
					if true then			
			--codes
				print("connected")
					end
		
			local start = task.spawn(gameLoop)
			
			if char:FindFirstChild("Humanoid").Health == 0 then
				task.cancel(start)
				print("cancelled")
			end

			while task.wait(0.04) do
				if char:FindFirstChild("Humanoid").Health ~= 0 then
-- codes				
	end
end)

Normal Script in Workspace:

local RS = game:GetService("ReplicatedStorage")
local intToE = script.Parent.ProximityPrompt

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	RS.StartGame:FireClient(player)
	RS.FitChar:FireClient(use,less,things)
end)


Try to move the local script in startergui

1 Like

Nothing changed dude, it won’t even print cancelled.

But it prints connected when game started…?

Check if the server player is actually near the the prompt when it’s clicked, and yes, you can print from a local script

Yes ofc, i use Hold 2 second and play. All works at first time until died and retried.

Do you have any errors in the console?

Not even one error… That’s why i don’t understand.

Check if character has pressed prompt

1 Like

image

Prob this isn’t working when you reset

Actually it works, because when it fired i tried to make humanoid.WalkSpeed = 0 and it works.

I see now, problem is second event which is FitChar

It won’t connect or fire it:

image

1 Like

But i still don’t know what’s wrong and how to fix this…

Try this I dont know if it will work

This is the local script:

local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

local function initiateGameLoop()
    local char = player.Character or player.CharacterAdded:Wait()
    local humanoid = char:FindFirstChildOfClass("Humanoid")

    local function gameLoop()
        while task.wait(1) do
            print("Game loop is running")
            -- Your game loop code here
        end
    end

    local start = task.spawn(gameLoop)

    local deathConnection
    deathConnection = humanoid.Died:Connect(function()
        task.cancel(start)
        print("Game loop cancelled due to death")
        deathConnection:Disconnect() -- Disconnect death listener
    end)
end

-- Listening for the start game event from the server
RS:WaitForChild("StartGame").OnClientEvent:Connect(function()
    initiateGameLoop() -- Call function to initiate game loop
end)

The server should be the same.

1 Like

The problem is not about script i guess, it won’t fire FitChar Event after died and played again.

Oh ok I understnd now. I´ll try to see why the event is not is not played

1 Like

The full code of Script in Workspace.Part:

local RS = game:GetService("ReplicatedStorage")
local intToE = script.Parent.ProximityPrompt

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	local npc = script.Parent.Parent:WaitForChild("NPC").HumanoidRootPart.Position
	local npcRoot = script.Parent.Parent:WaitForChild("NPC").HumanoidRootPart
	local npcpower = script.Parent.Parent:WaitForChild("NpcPower").Value
	intToE.Enabled = false
	RS.StartGame:FireClient(player)
	RS.FitChar:FireClient(player, npc, npcpower,npcRoot)
end)

When i joined game and pressed E, it fires both of event. After i died and clicked E again, it firest only StartGame, not FitChat.

  1. I don’t even understand why you would want to use 2 RemoteEvents in this scenario. Looks like one RemoteEvent will suffice.
  2. You are essentially “reconnecting” the second RemoteEvent whenever the first RemoteEvent gets fired.
  3. This pattern is prone to race conditions, especially with your WaitForChilds.

Why not do this instead:

-- Server
ProximityPrompt.Triggered:Connect(function(player)
    StartGame:FireClient(player, your, args)
end)

-- Client
StartGame.OnClientEvent:Connect(function(your, args)
    -- one is enough?
end)

If you really are insisting on doing back and forth communication, consider using RemoteFunctions instead.

2 Likes

Haha, yeah this one worked now! And i am a garbage scripter, i am just trying to learn and practice. I had problems with RemoteEvents at first time so i decided to fire 2x. Now everything works perfect! Thank you.