So I was trying to make a basic script that sends a message to all servers to kill all players, but Roblox seems to be getting stuck on a really dumb error. Why doesn’t it just ignore the camera because it doesn’t have a humanoid part in it?
The error is Humanoid is not a valid member of Camera "Workspace.Camera" Stack Begin Script'ServerScriptService.MessageSendAndReceiveScript', Line 5
I think it’s weird because it’s an if statement and it’s erroring. Why doesn’t it just continue?
(Line 5 is the if statement)
Also this only errors in live game because of the game.Players.PlayerAdded function
local MessagingService = game:GetService("MessagingService")
MessagingService:SubscribeAsync("GlobalAnnouncement",function(message)
for i, v in pairs(game.Workspace:GetChildren()) do
if v.Humanoid.Health ~= nil then
v.Humanoid.Health = 0
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.PlayerGui:WaitForChild("MainGui").TextButton.MouseButton1Click:Connect(function()
MessagingService:PublishAsync("GlobalAnnouncement","Hello there")
end)
end)
It is erroring because you are assuming every child of Workspace has a Humanoid child under it! It would error with a part also, nothing to do with the camera specifically.
If you wanted to make your script continue you’d have to do something like this:
for i, v in pairs(game.Workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
if v.Humanoid.Health ~= nil then
v.Humanoid.Health = 0
end
end
end
but you’d still be needlessly cycling through every item in the workspace searching for Humanoids
That’s a legitimate error. As mentioned earlier, you’re assuming that every instance in the Workspace has an immediate child named Humanoid with a Health property. Even if you fix the assumption that a Humanoid exists in every instance, the Health property existence assumption still exists.
If you just want to kill every player in the server when the subscription signal is fired, then use the Players service to get the existing players instead. You can then check their characters for a Humanoid object and if one exists, kill it.
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
local character = player.Character
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:ChangeState(Enum.HumanoidStateType.Dead)
end
end
Using ChangeState because there are some things that can bypass a health set (e.g. forcing the Humanoid’s health not to be 0 from HealthChanged).