Script not working

Ontouch the seat will go into replicated storage than when the player who touched it dies the seat goes back

script.Parent.Touched:connect(function(player)
	
		local humanoid = player:FindFirstChild ("Humanoid")
	task.wait()
	game.Workspace.LeaderSeat.Parent = game.ReplicatedStorage
	task.wait()
	
	if player.humanoid.Health <= 0 
	then
		task.wait()
		game.ReplicatedStorage.LeaderSeat.Parent = game.Workspace
		task.wait()
		
	end
end)
1 Like

Hook up a .Died function to their humanoid! Also make sure that you add them to a table or something so it doesn’t create a ton of .Died functions. But why are you reposting?

I probably should not have reposted but every time I try responding on other posts (due to the fact I already said there was a solution then removed) there are usually never any responses.

Here are some errors with the code.

.Touched’s first argument (hit) is the instance that fired this event. When a player touches something; with a .Touched event, it will normally trigger on the body part that triggered it, e.g: LeftArm.

Since the character is only a model, you tried to use a bodypart; and index to the humanoid, which is only parented to the model of the character.
So what you will do, is change it to this:

local humanoid = player.Parent:FindFirstChild("Humanoid") -- on the bodypart that triggered the event, use .Parent to go straight to the main model, then index to the humanoid.

----------------------------------------

As what @RefusalMan said, you will have to use .Died events for this, (and some variables too), best way to go by this is to use a table containing the RBXevents.
You could do this (finalized):

local connections = {}

local seat = workspace.LeaderSeat
local ready = true
local function clear()
   for i,v in next, connections do
      v:Disconnect()
   end
end

seat.Touched:Connect(function(Part)
if ready = true then
   local Humanoid = Part.Parent:FindFirstChild("Humanoid")
   local name = Part.Parent.Name

   if Humanoid then
      if Humanoid.Health >= 1 then
         ready = false
         seat.Parent = game.ReplicatedStorage
         table.insert(connections, Humanoid.Died:Connect(function()
         ready = true
         seat.Parent = workspace
         clear()
         end))
         for _, v in next, game.Players:GetPlayers()) do
            if v.Name == name then
               table.insert(connections, v.PlayerRemoving:Connect(function()
               ready = true
               ready = true
               seat.Parent = workspace
               clear()
               end))
            end
         end
      end
end)

Do whatever you want to manage the rest

local connections = {}

local seat = workspace.LeaderSeat
local ready = true
local function clear()
   for i,v in next, connections do
      v:Disconnect()
   end
end

seat.Touched:Connect(function(Part)
if ready == true then
   local Humanoid = Part.Parent:FindFirstChild("Humanoid")
   local name = Part.Parent.Name

   if Humanoid then
      if Humanoid.Health >= 1 then
         ready = false
         seat.Parent = game.ReplicatedStorage
         table.insert(connections, Humanoid.Died:Connect(function()
         ready = true
         seat.Parent = workspace
         clear()
         end))
         for _, v in next, game.Players:GetPlayers()) do
            if v.Name == name then
               table.insert(connections, v.PlayerRemoving:Connect(function()
               ready = true
               ready = true
               seat.Parent = workspace
               clear()
               end))
            end
         end
      end
end)

changed one line of code.
Error appearing. Not sure what this means, usually it means to remove the end (from experience) but not in this case. Thank you.
scriptchange

scripterror

edited my script a bit and got it working, anything I can improve?
added .died which ended up fixing the if statement.
``
script.Parent.Touched:connect(function(player)

	local humanoid = player.Parent:FindFirstChild ("Humanoid")
task.wait()
game.Workspace.LeaderSeat.Parent = game.ReplicatedStorage
task.wait()

if humanoid.died
then
	task.wait()
	game.ReplicatedStorage.LeaderSeat.Parent = game.Workspace
	task.wait()
	
end end)

moved the end) back one space so it fit in the font script thing

never mind just re-tested it.
It didn’t work.

That error triggers due to an ‘end’. If it errors due to an “)”,. It shouldn’t be there. I’ll look into it and fix whatever needs to be fixed.