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
function onTouched() if (HumanoidRootPart.Position - Rocket.Position).Magnitude <= 10 then local ForceField = Instance.new("ForceField") ForceField.Visible = false ForceField.Parent = Character game:GetService("Debris"):AddItem(ForceField,.5) end local function onHit(Part) local TargetCharacter = Part:FindFirstAncestorWhichIsA("Model") print("Test1") if game:GetService("Players"):GetPlayerFromCharacter(TargetCharacter) then local TargetHumanoid = TargetCharacter:FindFirstChild("Humanoid") CreatorValue:Clone().Parent = TargetHumanoid print("Test2") TargetHumanoid:TakeDamage(30) DebrisService:AddItem(CreatorValue) end end local Explosion = Instance.new("Explosion") Explosion.BlastPressure = 250000 Explosion.DestroyJointRadiusPercent = 0 Explosion.Position = Rocket.Position Explosion.Parent = workspace Explosion.Hit:Connect(onHit) Rocket:Destroy() TouchedEvent:Disconnect() end
FULL SCRIPT
local Rocket = script.Parent local CreatorValue = Rocket:WaitForChild("CreatorValue") local Player = CreatorValue.Value local Character = Player.Character or Player.CharacterAdded:Wait() local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") local TouchedEvent local DebrisService = game:GetService("Debris") function onTouched() if (HumanoidRootPart.Position - Rocket.Position).Magnitude <= 10 then local ForceField = Instance.new("ForceField") ForceField.Visible = false ForceField.Parent = Character game:GetService("Debris"):AddItem(ForceField,.5) end local function onHit(Part) local TargetCharacter = Part:FindFirstAncestorWhichIsA("Model") print("Test1") if game:GetService("Players"):GetPlayerFromCharacter(TargetCharacter) then local TargetHumanoid = TargetCharacter:FindFirstChild("Humanoid") CreatorValue:Clone().Parent = TargetHumanoid print("Test2") TargetHumanoid:TakeDamage(30) DebrisService:AddItem(CreatorValue) end end local Explosion = Instance.new("Explosion") Explosion.BlastPressure = 250000 Explosion.DestroyJointRadiusPercent = 0 Explosion.Position = Rocket.Position Explosion.Parent = workspace Explosion.Hit:Connect(onHit) Rocket:Destroy() TouchedEvent:Disconnect() end TouchedEvent = Rocket.Touched:Connect(onTouched)