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

Can someone tell what's wrong with this script? I'm trying to make a kill all button.


local victims = game.Players:GetChildren()
local char = victims.Character

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

script.Parent.MouseButton1Down:connect(function()
	char.Head:remove()
	print("You killed everyone!")
end)

I'm still learning and some help would be really useful!

1 answer

✓ Accepted answer

###This is because you're attempting to access a Character inside a table. You'll have to loop though the players to get their character and destroy the head.

###Also, you should use GetPlayers over GetChildren as GetChildren is prone to script error, should an object that isn't a Player reside in the Players service.

###Instance:remove() is deprecated, switch to Instance:Destroy(). RBXScriptSignal:connect() is also deprecated, use RBXScriptSignal:Connect() instead.

###The GuiButton.MouseButton1Down event runs code whilst the mouse is held down, so the code will keep running until the mouse is no longer held down. You'll want to use MouseButton1Click, unless that's the behaviour you want.

local victims = game.Players:GetPlayers()

script.Parent.MouseButton1Click:Connect(function()
    for i, v in pairs(victims) do -- iterate through each player
        v.Character.Head:Destroy()
        print("You killed everyone!")
    end
end)

3 comments

0

I would accept this answer - It's really helpful :D

0

Thanks for your answer! What is the meaning of that 4th line? I'm really new to scripting and it would be helpful if you could explain the parameters you used and the "for something in pairs... do".

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