If I create a part, add the script below as a child of that part, and Run the game without that part being anchored, it will print “hit” when the part touches something (which is what I expect).
script.Parent.Touched:Connect(function()
print(“hit”)
end)
However, if I spawn the part the script will be ignored entirely and it will not print “hit” when touched or perform anything else inside the function.
This is how I am spawning the part:
local toClone = game.ReplicatedStorage.Cloneables.ProjectileGG05
local newClone = toClone:Clone()
newClone.Parent = game.Workspace
newClone.Position = tool.Handle.Position
I am trying to make a cannon that fires a projectile which explodes upon impact, but it does not work because I must spawn the cannon ball, and that seems to make the script no longer work. I have also tried spawning the part and then cloning the script into that part afterwards, but it does not work that way either.
So when I did what you suggested (it worked) but it seemed to consider empty air as collideable. What I mean is, when I drop the part from the air as I mentioned in my first post, it will just send a single print message when it hits the ground. With the method I just now tried it sent over 300 print messages while it was flying through the air (it never made contact with any terrain). Is there any reason why creating these functions differently would affect whether or not it views air as a touchable item? For more clarity when I have an object in studio above the ground that is unanchored with its own touch script it will fall to the ground and print when it touches the ground. If I use a tool that spawns the same object in studio it will ignore the touch script and not fire. If I add newClone.Touched:connect (etc…) it seems to work, but it seems to interpret even empty space as an object it is hitting.
The hit events are probably from wherever the projectile is coming from. For example, if it’s coming from inside the player, it’ll collide with the player multiple times (probably).
That was just an example, what is happening is probably the same thing.
I would recommend checking if hit is a descendant of the cannon model.
local cannonModel = [Path to cannon model]
script.Parent.Touched:Connect(function(hit)
if not hit:IsDescendantOf(cannonModel) then
--Cannon ball has hit something valid, should explode.
end
end)
If this doesn’t solve your issue, you can print out hit to check what it is to figure out why it might be happening.
Thank you guys all VERY much. I will go try these out. I’m very sorry I forgot to clarify that the cannon ball is only a single part, so it doesn’t have any child parts it could hit. I don’t know if that is relevant but I wanted to mention it.
I have heard before that the order of Position and Parent matters but I did not remember the order. Thank you very much. Are there any situations in which position and parent order does not matter and you can therefore use Instance.new(“Explosion”, workspace)? Thank you!
Thank you very much for your reply. I tried this but I am still have the same issue as before. Using the above code, if I drop the ball from the air manually (by unanchoring it and having it fall) it works, but when I launch it from the cannon it does not. Could this have anything to do with the fact that I am using a Key Input in a LocalScript to Spawn a Cannon Ball? The spawn works, but could this be somehow disabling the script? Thank you!
So the issue there is the fact that a local script creates the cannon ball. This will not replicate to the server, and scripts do not run on the client, only the server. You could send a remote event to the server from the client to tell it to spawn the cannon ball. Or if this is meant to be a local only effect, then you would use a LocalScript in the cannonball, not a Script.
Thank you so much! I had learned about Remote Events previously but I (as a bad habit) often first make things without Remote Events in the design phase and then I later create the Remote Events. I thought that the script would be spawned or would work along with the cannon (in the studio mode) and that was the problem. Sorry guys I should have realized that. Thank you all very much it works now!
This seems like reasonable workflow to me – I do the same. Focus on getting a prototype of the feature that works without worrying about the implementation details of the rest of the project, and then split integration into a separate task. Perfect example of decomposition of the problem.
However now with the RemoteEvent and the ball spawning from a Server Script it ignores the location and LookVector of the “FirePlace” part which I use as the target point for spawning the cannon ball. So now regardless of how the cannon is orientated it spawns in the same place (which is the place where the “FirePlace” part was originally before moving it). I have tried moving the line where I declare the part “FirePlace” inside the Fire Function but that does not help. Does anybody have any ideas?