Local script/functions question

So basically. In a local script, I create a bullet. And then inside this bullet function, I then after creating the bullet try to attach a .touched event to the newly created bullet part. But when passing this bullet in the arguments, in the ontouched function the bullet part is now considered nil? Is this not possible?

So lets say it looks like this:

function onTouched(hit, bullet)
print(hit, bullet)
end

function fire()
bullet = Instance.new("Part")
bullet.Parent = workspace
bullet.Touched:connect(onTouched, bullet)
end

fire()

Output → “Part, nil”

In this case, you cannot pass another argument besides the touched object, to fix this, use another function inside of the connect and call the other function inside of it.

function onTouched(hit, bullet)
    print(hit, bullet)
end

function fire()
    bullet = Instance.new("Part")
    bullet.Parent = workspace
    bullet.Touched:Connect(function(hit)
        onTouched(hit, bullet)
    end)
end

fire()

Edit: nvm thx
//////////////./////////////

I’m getting this error actually:

Workspace.matt1020304050.control:323: attempt to call a Instance value

My code:


function hit(hit, Bullet)
-- blah
end

function fire()
a = Instance.new("Part)
a.Parent = workspace
a.Touched:Connect(function(hit)
	hit(hit, a)
	end)
end
fire()

the function is named the same as the part that touched the bullet (ignoring the string as I think that was just a typing error you made on devforum). Either rename the touched part or rename the function.

2 Likes

Thank you: https://gyazo.com/d8bc493cd91129672f88f2ae09950967

:smiley:

1 Like