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

Tool only works for one person at a time. How do I make it work for all players?

Right now my "Taser" only works for "Player1". How do I make it so it works for any player?

Tazer = game.StarterPack.Tazer.Handle 

TazerPlayer = game.Players.Player1.Backpack.Tazer.Handle -- how do I make this apply to any player.Backpack.Tazer.Handle? 

Robot = game.Workspace.Robot

RobotHealth = game.Workspace.Robot.Humanoid


function tazed(hit) 
	print("TAZED")
	print(hit.Name)
	hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10 
	hit.BrickColor = BrickColor.new("Teal")	
	hit.Sparkles.Enabled = true
	wait(0.8)
	hit.Sparkles.Enabled = false 
	hit.BrickColor = BrickColor.new("Black")
	if hit.Parent.Humanoid.Health < 1 then
		hit.Anchored = false 
	end
end

TazerPlayer.Touched:connect(tazed)

2 comments

0

Is it a local script?

0

It is not a local script

1 answer

Since it seems you're assigning these events through a server script (which I highly discourage!), the best way I can think of is assigning the events through a for i,v in pairs() loop.

Here's my example:

-- We should definitely start by relocating the variables we won't need to define multiple times within the loop we're going to create. Let's move your global variables and function before we create the loop to avoid complications.

Tazer = game.StarterPack.Tazer.Handle 

Robot = game.Workspace.Robot

RobotHealth = game.Workspace.Robot.Humanoid

function tazed(hit) 
    print("TAZED")
    print(hit.Name)
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10 
    hit.BrickColor = BrickColor.new("Teal") 
    hit.Sparkles.Enabled = true
    wait(0.8)
    hit.Sparkles.Enabled = false 
    hit.BrickColor = BrickColor.new("Black")
    if hit.Parent.Humanoid.Health < 1 then
        hit.Anchored = false 
    end
end

for i, player in pairs(game.Players:GetPlayers()) do -- Here's the nitty gritty stuff! We're grabbing a table of players that we'll iterate through to find the tools in their respective backpacks.
	local TazerPlayer = player.Backpack.Tazer.Handle -- View the reasoning below for changing this to local!
	TazerPlayer.Touched:connect(tazed) -- We're now assigning the event to this player's taser handle.
end

Here's why we're changing that variable to be localized, see scope.

Be aware that this only goes through the players one time! Perhaps if you're wanting the tools to work on every respawn, you'd have to work with some CharacterAdded events within the player. Here's the wiki documentation for that here

You can always send me a PM if this isn't sufficient to your needs or get help from another answer-er on here.

Happy coding, HollowMariofan

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