Basically, I just want to check if there are multiple stun BoolValues in a character, and if there are replace the current Stun BoolValue with a new one to replace it. Any way to do this with relative ease ( or not, I’m willing to do a little work for this to work )
for I,V in pairs(Character:GetChildren()) do --or whereever u are storing the values do
if V.Name == "stun"and V:IsA("BoolValue") then
V:Destroy()
end
local NewBoolVal = Instance.new("BoolValue")
NewBoolVal.Name = "stun"
NewBoolVal.Parent = Character
end
Easy.
local count = 0
for i,v in pairs(--[[PARENT]]) do
count = count + 1
end
if count > 1 then
print("There is more than one!")
end
@S0MBRX @kyler_josiah Why are you doing pairs
instead of ipairs
? That’s intended for dictionaries… Sure you’ll get the same result, but it’s better to stay consistent!
I tested this method for a little, and the current thing pretty much works, but it’ll stack around 4 stuns on top of each other making the character unable to move. Currently I’m going through a few tweaks, but I will try the other method that I saw from @kyler_josiah.
How would I be able to check the stun? The same way as the previous method shown above? I’ll check this way out though. Also, I’ll be using ipairs because I haven’t heard much about it, but if it’s useful for dictionaries I’ll check that out as well.
wdym my script literally loops through every item in the character its not going to stop after it removes one?
then adds a new one to the character
so you end up with one single new stun var
Yeah, I’m confused as well. I might assume it’s due to my way of using count, but I have been looking at the script and the more I do the less I begin to understand why it keeps duplicating.
well post the script so we can see whats actually going on
for i, parts in pairs (HBFinder) do
local enemyChar = parts.Parent
local enemyHumanoid = parts.Parent.Humanoid
print("What's going on?")
local M1Stun = Instance.new("BoolValue")
M1Stun.Name = "Stun"
M1Stun.Parent = enemyHumanoid
local enemyRootPart = parts.Parent.HumanoidRootPart
if Character:FindFirstChild("Stun") then print("You are stunned?!") return end
enemyHumanoid:TakeDamage(5)
enemyHumanoid.WalkSpeed = 4
enemyHumanoid.JumpPower = 0
Debris:AddItem(M1Stun, 0.25)
enemyHumanoid.WalkSpeed = 15
enemyHumanoid.JumpPower = 50.125
for I,V in pairs(enemyHumanoid:GetChildren()) do --or whereever u are storing the values do
if V.Name == "Stun"and V:IsA("BoolValue") then
V:Destroy()
end
local NewBoolVal = Instance.new("BoolValue")
NewBoolVal.Name = "Stun"
NewBoolVal.Parent = enemyHumanoid
Debris:AddItem(NewBoolVal, 0.25)
end
if Count == 4 then
local BV = Instance.new("BodyVelocity", parts.Parent.HumanoidRootPart)
BV.MaxForce = Vector3.new(22500, 22500, 22500)
BV.P = 33300
BV.Velocity = -parts.Parent.HumanoidRootPart.CFrame.LookVector * 40
Debris:AddItem(BV, 0.25)
end
break
end
(Probably will be made to be neater in the future once I fix this hopefully.)
because u are making a bool value for every part in the hitbox and there is more than one part in the hitbox
So, should there be a folder made inside of a character when they spawn in and have it hold the values instead?
no you need to make a variable for humanoid found so like
local HumanoidFound
for i,v in pairs hitbox --whatever
if HumanoidFound then
break
end
if v.parent == humanoid then
humanoidfound = true
end
--do the rest of the procedure
end
so that it will only run once through the character rather than for the number of humanoid parts
Ohhh, this is an interesting method. Thank you.
btw itll be V.Parent == Character.Name (chcaracter being the character you are looking for)
and also i missed the do on the end of the for loop its just some rough guidance code but should do the trick
That is just how I learned it.
That’s alright! Here’s an example of how to correctly use both of them.
ipairs
local Array = {"a", "b", "c"}
for index, value in ipairs(Array) do
print(index, value)
end
-- Outputs:
-- 1 a
-- 2 b
-- 3 c
pairs
local Dictionary = {
["first letter"] = "a",
["second letter"] = "b",
["third letter"] = "c"
}
for key, value in pairs(Dictionary) do
print(key, value)
end
-- Outputs:
-- first letter a
-- second letter b
-- third letter c
Here’s a test/place file if you’d like to mess around with this yourself:
ipairs and pairs test.rbxl (32.1 KB)
And if you’d like to read more: pairs and iPairs (education.roblox.com)
I’m a fellow ipairs() purist as well but to be fair the order in which instances are returned via “:GetChildren()”/":GetDescendants()" is random so iterating over them via ipairs() is essentially the same as iterating over them via pairs().
Although for arrays which are truly ordered I agree & personally stick with ipairs().
Doing this still yields the same results of four stun boolean values. How would I actually use HumanoidFound?