I’ve been trying to make a zombie shooter game, but the damage system isn’t working correctly, everything else works perfectly fine. I kind of understand why it isn’t working, because the touched function is only firing when the bullet is shot, but I don’t understand where else to put it. Help would be greatly appreciated!
This is inside of a module script
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local viewModels = workspace:WaitForChild("ViewModels")
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local guns = ReplicatedStorage:WaitForChild("Guns")
local remotes = ReplicatedStorage:WaitForChild("Remotes")
local gunConnection = nil
local playingGunHoldAnimation = nil
local viewModel = nil
local isAiming = false
local isFiring = false
local isGunEquipped = false
local shootDebounce = false
local bulletDebounce = false
local reloadDebounce = false
local config = nil
local lastShootTick = tick()
local module = {}
local function DamageVictim(humanoid)
if humanoid then
humanoid.Health = humanoid.Health - config.Damage
end
end
local function Shoot()
if config.CurrentMagazine <= 0 then
warn("Reload before you can shoot again")
return
end
if (tick() - lastShootTick) > config.Firerate then
local bulletConnection = nil
local bullet = config.Bullet:Clone()
bullet.CFrame = CFrame.new(viewModel.BulletLocation.Position, mouse.Hit.Position)
bullet.Parent = workspace.Bullets
config.CurrentMagazine = config.CurrentMagazine - 1
config.ShootSound:Play()
local cframe = CFrame.new()
bulletConnection = RunService.Heartbeat:Connect(function()
if not isGunEquipped then
return
end
cframe = bullet.CFrame + bullet.CFrame.LookVector * config.BulletSpeed
bullet.CFrame = bullet.CFrame:Lerp(cframe, 0.1)
end)
bullet.Touched:Connect(function(hit)
if hit.Parent.Name == player.Name then
return
end
if hit.Parent:FindFirstChildOfClass("Humanoid") then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
bullet:Destroy()
DamageVictim(humanoid)
end
end)
task.delay(5, function()
if bullet.Parent ~= nil then
bullet:Destroy()
end
end)
lastShootTick = tick()
else
return
end
end
the DamageVictim function is to take away health, obviously, but it only works correctly if I am right up to the dummy, far away shots do not damage the victim.
Thank you for your time.