I am trying to print a message that a player clicked the button then do 500 damage to all players. Please explain and help me figure out why this isn't working.
local M = Instance.new("Message", game.Workspace) local p = game.Players:GetChildren() function onClicked() M.Text = "The button was clicked by .. " .. game.Players.LocalPlayer.Character.Name wait(2) M:remove() for i = 1, #p do p[i].Character.Humanoid:TakeDamage(500) end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
I figured it out. Here is the updated working code.
function onClicked(player) local p = game.Players:GetPlayers() local M = Instance.new("Message", game.Workspace) wait(0.5) M.Text = "The button was clicked by .. " .. player.Name wait(2) M:remove() for i = 1, #p do if p[i] then p[i].Character:WaitForChild('Humanoid'):TakeDamage(500) end end end print("Kill button script ended") script.Parent.ClickDetector.MouseClick:connect(onClicked)