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