local rs = game:GetService("ReplicatedStorage") local gunEvent = rs:WaitForChild("GunEvent") local gunsound = game:GetService("StarterPack") local shootingsound = gunsound.Tool.Part.Sound gunEvent.OnClientEvent:Connect(function(player, startPos, endPos) shootingsound:Play() local part = Instance.new("Part") part.Size = Vector3.new(.25,.25,10) part.CFrame = CFrame.new(startPos, endPos) local bv = Instance.new("BodyVelocity", part) bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge) bv.Velocity = part.CFrame.LookVector * 800 part.Parent = game.Workspace.CurrentCamera part.CanCollide = false part.Material = ("Neon") part.BrickColor = BrickColor.new("New Yeller") local function onTouch(hit) if (hit.Parent:findFirstChild("Humanoid") ~= nil) then print(hit) print("Hit!") hit.Parent.Humanoid.Health = 0 part.Touched:connect(onTouch) delay(1, function() part:Destroy() end) end end end)
I am having trouble with this script. I coded it so that it would print "Hit!" in the output, and it doesn't. There are no errors in the output so I am stuck on this one. How do I make it so that when a bullet hits a humanoid, it prints "Hit!"..? What am I doing wrong?
BEFORE CHANGES (original script): https://gyazo.com/f67f6219f411ac944532c7e5d099e363
AFTER CHANGES (after i attempted to make a hit detection): https://gyazo.com/5dcab6a86e787c4816e5e0c5856ea0bb
1
I guess that its local script because you use OnClientEvent
.
If its server script you need this OnServerEvent
.
2
Tool in StarterPack will be in player's backpack. Change it to this to locate the sound.
local shootingsound = Player.Backpack:FindFirstChild("Tool").Part.Sound or Player.Character:FindFirstChild("Tool").Part.Sound
3
You only put function but not run the function. Move this line out of function
part.Touched:Connect(onTouch)
A following scripting line must be added. The line:
wait(1)