Hey there, I am trying to script a relatively simple bullet script for a (kinda) gun.
I am having problems with my bullet script tho ;( Or to be more precise: The script that registers when the bullet touches something, and if it is a humaniod - and so on.
Atm, I have two scripts:
The "GunKeyPressing script" (where there shouldn't be a problem), is a Local Script in the Starterpack that registers whenever I click the "f" key, and then clones a bullet, parents it to the Workspace (and so on). And then I have the "BulletDamage script", which is the one I am having trouble with. It gets successfully transported and enabled inside the bullet (I have it disabled until it is parented to the bullet-part - then I enable it), but the script itself doesn't actually seem to work (I tried setting a print-statement at the top of the script). Atm, I have it stored under the GunKeyPressing script (which was in the Starterpack), but I have also tried storing it in the ServerScriptService and ReplicatedStorage - without much luck either.
Here is the GunKeyPressing script (which I don't think there is anything wrong with):
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local bullet = game.ReplicatedStorage:FindFirstChild("Bullet") -- the actual bullet (just a black ball) local bodyForce = bullet.BulletBodyForce local barrel = game.Workspace.PlaneGun.Barrel bodyForce.Force = barrel.CFrame.LookVector * 400 local cooldown = false mouse.KeyDown:Connect(function(key) if key == "f" then if cooldown == false then if bullet then -- if we found the bullet in "ReplicatedStorage" cooldown = true local bulletCopy = bullet:Clone() local bulletDamage = script:FindFirstChild("BulletDamage") -- as I mentioned, the "BulletDamage script" is parented to the "GunKeyPressing Script". game.Debris:AddItem(bulletCopy, 100) bulletCopy.Parent = game.Workspace bulletCopy.Position = game.Workspace.PlaneGun.FirePosition.Position -- we are just positioning the bullet. if bulletDamage then local bulletDamageCopy = bulletDamage:Clone() bulletDamageCopy.Parent = bulletCopy bulletDamageCopy.Disabled = false -- And then we activate it after having moved it (it started of by being disabled). end wait(0.5) -- cooldown time cooldown = false else print("Bullet was not found") end end end end)
And now to "BulletDamage" script, where the script doesn't wanna run:
print("hi") -- just to test if there is any contact (which there is not atm) if script.Disabled == false then script.AncestryChanged:Connect(function () -- I never used this function before, so I might have used it incorrectly. if script.Parent.Name == "Bullet" then local bulletCopy = script.Parent bulletCopy.Touched:Connect (function (hit) local humaniod = hit.Parent:FindFirstChild("Humaniod") local firePos = hit.Parent:FindFirstChild ("FirePosition") -- This is just the part where the bullet "spawns", so I don't wanna make that count as a touch. if not firePos then if humaniod then -- insert stuff end end end) end end) end
Thanks in advance! Any kind of help is much appreciated :-)