Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

if player i spectate die it dont reset it is on place where he died, how to fix it?

to spectate player again i must click back and next again. pls help me 0:)

local debounce = false
local cam = game.Workspace.CurrentCamera
----------------
local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.SpectateButton

----------------
local players = nil
local number = 1
local maxnumber = 0
local player = game.Players.LocalPlayer
local lastplayer = player.Name
local activation = player:WaitForChild("Activation")
----------------
local playerMod = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerMod:GetControls()
---------------- right >
nex.MouseButton1Click:Connect(function(right)
	if debounce == true then
		players = game.Players:GetChildren()
		number = number + 1
		if players[number] ~= nil then
			game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
			lastplayer = players[number].Name
			print(lastplayer)
			if players[number].Name ~= player.Name then

				script.Parent.Bar.StagesValue.Texta.Text = ":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value

				players[number]:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
					script.Parent.Bar.StagesValue.Texta.Text =":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value
				end)



				script.Parent.Bar.Title.Texta.Text = players[number].Name
			else 
				script.Parent.Bar.StagesValue.Texta.Text = script.Parent.StagesValue.Texta.Text
				script.Parent.Bar.Title.Texta.Text = "You"
			end
		elseif players[number] == nil then
			number = 1
			game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
			lastplayer = players[number].Name
			if players[number].Name ~= player.Name then

				script.Parent.Bar.StagesValue.Texta.Text = ":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value

				players[number]:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
					script.Parent.Bar.StagesValue.Texta.Text =":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value
				end)

				script.Parent.Bar.Title.Texta.Text = players[number].Name
			else 
				script.Parent.Bar.StagesValue.Texta.Text = script.Parent.StagesValue.Texta.Text
				script.Parent.Bar.Title.Texta.Text = "You"
			end
		end
	end
end)
-- left <
prev.MouseButton1Click:Connect(function(left)
	if debounce == true then
		players = game.Players:GetChildren()
		maxnumber = #players
		number = number - 1
		if players[number] ~= nil then
			game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
			lastplayer = players[number].Name
			print(lastplayer)
			if players[number].Name ~= player.Name then

				script.Parent.Bar.StagesValue.Texta.Text = ":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value

				players[number]:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
					script.Parent.Bar.StagesValue.Texta.Text =":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value

				end)
				script.Parent.Bar.Title.Texta.Text = players[number].Name
			else 
				script.Parent.Bar.StagesValue.Texta.Text = script.Parent.StagesValue.Texta.Text
				script.Parent.Bar.Title.Texta.Text = "You"
			end
		elseif players[number] == nil then
			number = maxnumber
			game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
			lastplayer = players[number].Name
			if players[number].Name ~= player.Name then

				script.Parent.Bar.StagesValue.Texta.Text = ":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value

				players[number]:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
					script.Parent.Bar.StagesValue.Texta.Text =":checkered_flag: Stage: "..players[number]:WaitForChild("leaderstats"):WaitForChild("Stage").Value
				end)

				script.Parent.Bar.Title.Texta.Text = players[number].Name
			else 
				script.Parent.Bar.StagesValue.Texta.Text = script.Parent.StagesValue.Texta.Text
				script.Parent.Bar.Title.Texta.Text = "You"
			end
		end
	end
end)
--- on
button.MouseButton1Click:Connect(function(click)
	if debounce == false and activation.Value == false then debounce = true
		controls:Disable()
		activation.Value = true
		script.Parent.Bar.StagesValue.Texta.Text = script.Parent.StagesValue.Texta.Text
		bar:TweenPosition(UDim2.new(0, 0, 0, 0),"In","Linear",1,true)
	elseif debounce == true and activation.Value == true then debounce = false
		controls:Enable()
		activation.Value = false
		script.Parent.Bar.Title.Texta.Text = "You"
		lastplayer = player.Name
		script.Parent.Bar.StagesValue.Texta.Text = script.Parent.StagesValue.Texta.Text
		game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		number = 1
		bar:TweenPosition(UDim2.new(1, 0, 0, 0),"In","Linear",1,true)
	end
end)

1 answer

Create a listener for when the humanoid dies, then wait for the player's character to be added again, then set it to the new humanoid.

local runService = game:GetService('RunService')

-- ...

workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
players[number].CharacterAdded:Connect(function(character)
    repeat runService.RenderStepped:Wait() until character:IsDescendantOf(game)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.