So my goal is to give every player who has a boolvalue with its value set to true an explosion so they can die.
The code below does that but there’s one huge issue:
If there is more than 1 player who has their value set to true, it will give each player 2 explosions since the for loop will run twice.
How can I make it so no matter how many people have their state set to true,
it will just give each of them 1 explosion instance parented inside their character?
I’ve tried doing a checking statement but I haven’t had any luck with that.
Any help would be appreciated!
for _, v in pairs(game.Workspace:GetDescendants()) do
if v:IsA("BoolValue") and v.Name == "state" and v.Value == true then
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 1
explosion.BlastPressure = 10
explosion.ExplosionType = Enum.ExplosionType.NoCraters -- damages terrain
explosion.Position = v.Parent.Head.Position
explosion.Parent = v.Parent
end
end
local Players = {}
for _, v in pairs(game.Workspace:GetDescendants()) do
if v:IsA("BoolValue") and v.Name == "state" and v.Value == true and not table.find(Players, v.Parent.Name) then
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 1
explosion.BlastPressure = 10
explosion.ExplosionType = Enum.ExplosionType.NoCraters -- damages terrain
explosion.Position = v.Parent.Head.Position
explosion.Parent = v.Parent
table.insert(Players, v.Parent.Name)
end
end
end
And then reset the players table using table.clear once you’re done with the function.
what about parenting the bool under the player and then using game.Players:GetPlayers(), you can then loop through every player and get the bool there
or another option is to keep the bool under the character and then using game.Players:GetPlayers(), you can then loop through every player and get the bool using Player.Character
I recommend this because workspace:GetDescendants() has to loop through a lot compared to looping through ever player, this can help performance
You don’t have to loop through the whole workspace to do this.
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
local state = player.Character:FindFirstChild("state")
if state and state.Value then -- you probably should parent the bool to the player instead of the character, by the way
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 1
explosion.BlastPressure = 10
explosion.ExplosionType = Enum.ExplosionType.NoCraters -- damages terrain
explosion.Position = player.Character.Head.Position
explosion.Parent = player.Character
end
end
Since it’s a value it’s more convenient and efficient to use signals. Assuming that you’re looping that in an infinite loop it will run in the background draining performance. If you’re wondering how to even get the values there are also events that detect if a child has been added.
Hey, so I followed what you wrote, parenting the bool to the player instead of the character,
but for some reason every time I die and respawn, it’s still duplicating the explosions after every respawn.
I dug deeper into my code and checked that every time I respawn, a new player is being added (which is myself in this case) which makes 0 sense. That’s why the for loop is running numerous times and giving me more than 1 explosion instance.
Do you have any idea what might be causing this?
game.ReplicatedStorage.explode.explode.OnServerEvent:Connect(function(player, timerGuiText)
clearfeedEvent:FireAllClients()
timerGuiText.Text = "Kaboom!"
for _, player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
print(character) -- for some reason everytime I respawn this doubles, or triples, it makes no sense, why?
end
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
local state = player:FindFirstChild("state")
if state and state.Value == true then
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 1
explosion.BlastPressure = 10
explosion.ExplosionType = Enum.ExplosionType.NoCraters -- damages terrain
explosion.Position = player.Character.Head.Position
explosion.Parent = player.Character
else
print("nothnig")
end
end
end)