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)
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