Why is it that my Rocket Launcher's Explosion Hit Detection is not firing?
Asked by
5 years ago Edited 5 years ago
I'm trying to make a Rocket Launcher, and basically I want it so that when someone is hit by an explosion (detected via the Hit event), it will damage their humanoid by 30 and then proceed to insert a "CreatorValue" to log that a person killed them (in a future script).
The entire script is listed below, but the most important snippet of code to check over would be the touched event that fires.
The following scripts are contained in a Server-Side script that is cloned into a Rocket that is spawned in another script that is connected to a remote event responsible for spawning the rocket.
The "onTouched()" function fires when the rocket is touched. Whne onTouched is called, it runs a conditional statement that checks if the HumanoidRootPart is near the Rocket, so it can insert a forcefield to allow the player to rocket jump. Afterwards, it spawns in an explosion and tweaks its properties. It then creates an event that fires when something touches and explosion, which is then calls the function "onHit". onHit() basically gets the character and it goes through a conditional statement checking if the character it hit is actually a character and not something part of the map. If it returns as true, it gets the Humanoid and inserts a CreatorValue and makes the humanoid take damage.
The problem is that the function is not being called, and whenever i check the output to see if the dummy text has been printed, nothing appears.
CODE SNIPPET
02 | if (HumanoidRootPart.Position - Rocket.Position).Magnitude < = 10 then |
03 | local ForceField = Instance.new( "ForceField" ) |
04 | ForceField.Visible = false |
05 | ForceField.Parent = Character |
06 | game:GetService( "Debris" ):AddItem(ForceField,. 5 ) |
09 | local function onHit(Part) |
10 | local TargetCharacter = Part:FindFirstAncestorWhichIsA( "Model" ) |
14 | if game:GetService( "Players" ):GetPlayerFromCharacter(TargetCharacter) then |
15 | local TargetHumanoid = TargetCharacter:FindFirstChild( "Humanoid" ) |
17 | CreatorValue:Clone().Parent = TargetHumanoid |
21 | TargetHumanoid:TakeDamage( 30 ) |
22 | DebrisService:AddItem(CreatorValue) |
26 | local Explosion = Instance.new( "Explosion" ) |
27 | Explosion.BlastPressure = 250000 |
28 | Explosion.DestroyJointRadiusPercent = 0 |
29 | Explosion.Position = Rocket.Position |
31 | Explosion.Parent = workspace |
33 | Explosion.Hit:Connect(onHit) |
38 | TouchedEvent:Disconnect() |
FULL SCRIPT
01 | local Rocket = script.Parent |
02 | local CreatorValue = Rocket:WaitForChild( "CreatorValue" ) |
04 | local Player = CreatorValue.Value |
05 | local Character = Player.Character or Player.CharacterAdded:Wait() |
06 | local HumanoidRootPart = Character:WaitForChild( "HumanoidRootPart" ) |
10 | local DebrisService = game:GetService( "Debris" ) |
13 | if (HumanoidRootPart.Position - Rocket.Position).Magnitude < = 10 then |
14 | local ForceField = Instance.new( "ForceField" ) |
15 | ForceField.Visible = false |
16 | ForceField.Parent = Character |
17 | game:GetService( "Debris" ):AddItem(ForceField,. 5 ) |
20 | local function onHit(Part) |
21 | local TargetCharacter = Part:FindFirstAncestorWhichIsA( "Model" ) |
25 | if game:GetService( "Players" ):GetPlayerFromCharacter(TargetCharacter) then |
26 | local TargetHumanoid = TargetCharacter:FindFirstChild( "Humanoid" ) |
28 | CreatorValue:Clone().Parent = TargetHumanoid |
32 | TargetHumanoid:TakeDamage( 30 ) |
33 | DebrisService:AddItem(CreatorValue) |
37 | local Explosion = Instance.new( "Explosion" ) |
38 | Explosion.BlastPressure = 250000 |
39 | Explosion.DestroyJointRadiusPercent = 0 |
40 | Explosion.Position = Rocket.Position |
42 | Explosion.Parent = workspace |
44 | Explosion.Hit:Connect(onHit) |
49 | TouchedEvent:Disconnect() |
52 | TouchedEvent = Rocket.Touched:Connect(onTouched) |