Hello, I’m trying to make a function trigger whenever an humanoid comes in touch with a part, but I’m not really familiar with how the interaction between an humanoid and a part work since I’m kinda new to scripting. Could anyone try to figure out what’s wrong with this? I understand that the issue is at the hitbox.touched part, I’ve tried many alternatives but they don’t seem to work (bulding1 is welded btw).
speed = script.Parent.Hitbox.BodyVelocity
building = script.Parent.Parent.Building1
hitbox = script.Parent.Hitbox
function collapse ()
for _,v in pairs(building:GetChildren()) do
if v:IsA("Part") or v:IsA("BasePart") then
v.CanCollide = false
speed.Velocity = Vector3.new(0 , -3 , 0)
end
end
end
hitbox.Touched:Connect()
if hitbox.Parent:FindFirstChild("Humanoid") then
collapse ()
end
The hitbox will not be parented to the object that hits it. What you instead should try to do is use the variable .Touched returns. For example:
hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
collapse ()
end
end)
local speed = script.Parent.Hitbox.BodyVelocity
local building = script.Parent.Parent.Building1
local hitbox = script.Parent.Hitbox
function collapse ()
for _,v in pairs(building:GetChildren()) do
if v:IsA("Part") or v:IsA("BasePart") then
v.CanCollide = false
speed.Velocity = Vector3.new(0 , -3 , 0)
end
end
end
hitbox.Touched:Connect(function())
if hitbox.Parent:FindFirstChild("Humanoid") then
collapse ()
end
end)
Also use local variables instead of Global unless you’re changing things since local variables are faster
“The reason why you see local variables having faster lookup times is because internally it’s an arrway index. It just indexs from the Lua stack, which is on the heap. Alternativley, if abc was a global, a GETGLOBAL instruction would be produced, which does a dictionary loop-up over the environment table that getfenv() normally returns.”
It’s not mainly of local variables being allocated in an array in C, it’s mainly about less instructions being invoked when working with them as well.
Working with global variables invokes more instructions and a dictionary index is negligibly slower.
if abc was a global, a GETGLOBAL instruction would be produced,
Needs more clarification. GETGLOBAL is only invoked when you try to use an global variable.
a = 5
print(a) --> GETGLOBAL instruction is invoked, finds the global variable "a"
a += 5--> GETGLOBAL instruction is also invoked, finds the global variable "a" and another instruction is invoked for adding the global variable by 5
hitbox = script.Parent.Hitbox
speed = hitbox.BodyVelocity
building = script.Parent.Parent.Building1
hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
for _,v in pairs(building:GetChildren()) do
if v:IsA("BasePart") then
v.CanCollide = false
speed.Velocity = Vector3.new(0 , -3 , 0)
end
end
end
end)