What do you want to achieve? Keep it simple and clear!
I am trying to check if a bullet, that is being moved by tween service, is hitting any object at all
What is the issue? Include screenshots / videos if possible!
The issue is that the script won’t run as intended and doesn’t give me any errors either.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried making a ray cast but that won’t work since I am trying to see if the hits anything at its current orientation. I cannot find any other solutions
This is my code for the bullet, I am trying to see if it hits anything at all (other than my tool or myself)
PS: The bullet part starts out in the Server Storage, then is cloned, then the clone’s CFrame is adjusted, then it is shot out with tween service
local bullet = script.Parent
bullet.Touched:Connect(function(hit)
print(hit.Parent)
end)
It looks like you are trying to use the Touched event to detect if the bullet has hit anything. The Touched event fires when any part of the bullet comes into contact with another part, and it will pass the other part as an argument to the event callback function.
You can use this event to check if the bullet has hit a specific object, or you can use it to check if the bullet has hit any object at all.
To check if the bullet has hit any object at all, you can simply check if the hit argument is nil . If it is nil , then the bullet has not hit any object. If it is not nil , then the bullet has hit an object.
Here is an example of how you could use the Touched event to check if the bullet has hit any object:
bullet.Touched:Connect(function(hit)
if hit then
print("Bullet hit an object!")
else
print("Bullet did not hit any object.")
end
end)
I hope this helps! Let me know if you have any other questions.
I highly recommend using the ZonePlus Module for your case.
You’d need to create a zone around the bullet, and then check if an object enters the zone, which means you have a collision. It sounds complicated, but really, its only a few lines of code.
The larger the zone is around the bullet, the more leeway you’ll get, but the less accurate it’ll be.
I know how to the touched function works, seen in the code I added. The problem is, my bullets are visually colliding with other parts but nothing is being printed.
I slowed down the tween to 10 seconds but it still doesn’t work. I even shot parts at point blank range. Do you think I should try a different method? The reason I’m checking if the bullets hit is because I’m making a shotgun which shoots out 5 bullets at a slightly random angle.
I tried this and it still won’t work. Do you think it might be because of the properties of the bullet? CanCollide is on and it is also anchored. Do you have any other ideas? The reason I’m checking if the bullet was touched is because I’m trying to make a shotgun that shoots 5 bullets at a slight random angle, so I don’t think I can use ray casting right now.
You’re using TweenService, while the part is anchored?.Touched events do not fire unless it is unanchored touching something, or something unanchored is touch it.
local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = script.Parent
local zone = Zone.new(container)
zone.playerEntered:Connect(function(player)
print(("%s entered the zone!"):format(player.Name))
end)
zone.playerExited:Connect(function(player)
print(("%s exited the zone!"):format(player.Name))
end)
But it won’t print anything… Is it because both parts are anchored?
workspace:Raycast(
startPos, -- Vector3 where you're shooting from
direction, -- Vector3 relative to where you're shooting from, with it's length being how far it should check for
raycastParams -- optional RaycastParams
)
So if you want to raycast from a part 100 studs forwards you would do something like this
-- filter to ignore the part that's shooting the ray
local filter = RaycastParams.new()
filter.FilterType = Enum.RaycastFilterType.Blacklist
filter.FilterDescendantsInstances = {part}
-- or you can just make it non collide and not queryable
workspace:Raycast(
part.CFrame.Position,
part.CFrame.LookVector * 100,
filter
)
If you just want to raycast between 2 points, you can calculate the direction like so
local direction = endPos - startPos
Since you are tweening the bullet I assume that it isn’t moving infinitely fast, so you would need to do multiple raycasts along it’s path. Then it would also be easier to tween the bullet yourself.