I have small problem with spectategui, after death player, I can’t spectate him, how to fix it?
local frame = script.Parent.Frame
local title = frame.PlrName
local left = frame.Left
local right = frame.Right
local cam = workspace.CurrentCamera
local num = 1
local plr = game.Players.LocalPlayer
local function setCameraSubject(player)
if player and player.Character and player.Character:FindFirstChild("Humanoid") then
cam.CameraSubject = player.Character.Humanoid
title.Text = player.Name
else
cam.CameraSubject = nil
title.Text = "Player not available"
end
end
button.MouseButton1Click:Connect(function()
if frame.Visible then
frame.Visible = false
cam.CameraSubject = nil
else
frame.Visible = true
setCameraSubject(plr)
end
end)
right.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num + 1
if num > all then
num = 1
end
setCameraSubject(players[num])
end)
left.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num - 1
if num < 1 then
num = all
end
setCameraSubject(players[num])
end)
local frame = script.Parent.Frame
local title = frame.PlrName
local left = frame.Left
local right = frame.Right
local cam = workspace.CurrentCamera
local num = 1
local plr = game.Players.LocalPlayer
local connection = nil
local function setCameraSubject(player)
if player and player.Character and player.Character:FindFirstChild("Humanoid") then
if connection then connection:Disconnect() end
connection = player.CharacterAdded:Connect(function(new)
cam.CameraSubject = new.Humanoid
end)
cam.CameraSubject = player.Character.Humanoid
title.Text = player.Name
else
cam.CameraSubject = nil
title.Text = "Player not available"
end
end
button.MouseButton1Click:Connect(function()
if frame.Visible then
frame.Visible = false
cam.CameraSubject = nil
else
frame.Visible = true
setCameraSubject(plr)
end
end)
right.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num + 1
if num > all then
num = 1
end
setCameraSubject(players[num])
end)
left.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num - 1
if num < 1 then
num = all
end
setCameraSubject(players[num])
end)
Well I assume that when the player (or character) dies you don’t update that in your frame.
i.e. The script is still working off of the previous characters subject, not the new ones.
You can either do a loop or just have a event set up for when the characters.Humanoid.Died event happens, then wait for the new character, and use that.
local frame = script.Parent.Frame
local title = frame.PlrName
local left = frame.Left
local right = frame.Right
local cam = workspace.CurrentCamera
local num = 1
local plr = game.Players.LocalPlayer
local subject = plr
local function setCameraSubject(player)
if player and player.Character and player.Character:FindFirstChild("Humanoid") then
subject = player
title.Text = player.Name
else
subject = nil
title.Text = "Player not available"
end
end
button.MouseButton1Click:Connect(function()
if frame.Visible then
frame.Visible = false
cam.CameraSubject = nil
else
frame.Visible = true
setCameraSubject(plr)
end
end)
right.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num + 1
if num > all then
num = 1
end
setCameraSubject(players[num])
end)
left.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num - 1
if num < 1 then
num = all
end
setCameraSubject(players[num])
end)
game:GetService("RunService").Heartbeat:Connect(function()
if subject and workspace:FindFirstChild(subject.Name) then
cam.CameraSubject = subject.Character.Humanoid
else
cam.CameraSubject = nil
end
end)
local frame = script.Parent.Frame
local title = frame.PlrName
local left = frame.Left
local right = frame.Right
local button = script.Parent.OpenSpectate
local cam = workspace.CurrentCamera
local num = 1
local plr = game.Players.LocalPlayer
local subject = plr
-- Function to update camera subject and player name
local function setCameraSubject(player)
if player and player.Character and player.Character:FindFirstChild("Humanoid") then
subject = player
title.Text = player.Name
cam.CameraSubject = player.Character.Humanoid
else
subject = nil
title.Text = "Player not available"
cam.CameraSubject = nil
end
end
-- Function to handle player character respawn
local function onCharacterAdded(character)
if character:FindFirstChild("Humanoid") then
character.Humanoid.Died:Connect(function()
setCameraSubject(nil) -- Reset camera subject when a player dies
end)
end
end
-- Connect button click to toggle spectate frame visibility
button.MouseButton1Click:Connect(function()
if frame.Visible then
frame.Visible = false
cam.CameraSubject = nil
else
frame.Visible = true
setCameraSubject(plr)
end
end)
-- Move to the next player in the list
right.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num + 1
if num > all then
num = 1
end
setCameraSubject(players[num])
end)
-- Move to the previous player in the list
left.MouseButton1Click:Connect(function()
local players = game.Players:GetPlayers()
local all = #players
num = num - 1
if num < 1 then
num = all
end
setCameraSubject(players[num])
end)
-- Update camera subject during the game loop
game:GetService("RunService").Heartbeat:Connect(function()
if subject and subject.Character and subject.Character:FindFirstChild("Humanoid") then
cam.CameraSubject = subject.Character.Humanoid
else
cam.CameraSubject = nil
end
end)
-- Listen for player character added event to handle respawn
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(onCharacterAdded)
end)
-- Also connect for the local player in case it's the same player
plr.CharacterAdded:Connect(onCharacterAdded)