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

How to access all players?

I was wondering, Does this script work? And how do you access all players for like teleporting all players and killing all players? (And how do you access a players backpack? Is it .backpack?) Script :

--[[ To leave comments type -- then your messsage.
	ex. --Add a timer. Put it next to the line you want me to add it to.
]]--
--[[VARIABLES]]--
timertext = script.Parent.Text
mappicked = script.Parent.hasPickedMap
intermissiontime = 20
timertime = 210
playersjoin = game.Players.NumPlayers
--[[FUNCTIONS]]--
--function countplayers
if playersjoin == 4 then
	--function intermission
	repeat 
		wait(1)
		intermissiontime = intermissiontime - 1
		timertext = intermissiontime
	until
	intermissiontime == 0
	--function choosemap
	timertext = "Choosing map..."	
	wait(1)	
	map = math.random(game.Workspace.Folder)
	timertext = map
	map:clone(game.workspace)
	map.Position = "0,0,0"
	--function teleplayers
	timertext = "Teleporting players..."
	--teleportplayers
	--function choosemurderer
	murder = math.random(game.Players)
	game.ServerStorage["Murderer Knife"]:clone(murder.Backpack)
end

Sorry for having a lot of questions, but also, how do you refresh a script, like, make the script reset and run again?

4 comments

0

If you are trying to make a murder game you should watch the tutorial from the Roblox University. We aren't going to recreate a whole murder game for you.

0

I'm not asking for someone to recreate it for me. -_- I'm just asking some questions.

0

In addition to the answers below, do consider making sure each field exists. ex, If you say "v.Character.Humanoid", this will error if v.Character is nil (might happen if the character hasn't loaded yet).

0

Yes true but they will most likely be loaded then, but yes, I will make sure

2 answers

✓ Accepted answer

An easier way to do it is to use a for loop, example:

for i,v in pairs(game.Players:GetPlayers()) do
v.--Whatever you want to do!
end

Hope this helps, if it does, please accept my answer, it gives us both rep!

2 comments

0

So, like this? for i,v in pairs(game.Players:GetPlayers()) do v.Character.Humanoid.Health = 0 end

0

Exactly!

To access all the players you can do this:

local p = game.Players:GetChildren()

for i = 1,#p do
	p[i].CharacterAdded:wait()
	p[i].Character.Humanoid.Health = 0
end

This is the simplest version.

To reset a script, you could

1. Clone it and put in the script's parent, then remove() the script.

2. Make the entire thing a while loop.

2 comments

0

Line 4 will wait for the player to respawn (which might take minutes!). Instead, you should simply make sure that the Character exists and skip over them if they don't.

0

True. But I was just showing him how. He should've accepted my answer, cause it was the simplest and easiest to learn :/

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