So I was working on an fps system, and when the humanoid is supposed to take “25” damage when they get shot, the hit function gets fired multiple times so the humanoid ends up getting one shot, is there any way I could fix this?
local fireEvent = game.ReplicatedStorage.FireEvent
local FastCast = require(game.ReplicatedStorage.FastCastRedux)
local bulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
bulletsFolder.Name = "BulletFolder"
local bulletTemplate = game.ReplicatedStorage.Bullet:Clone()
--FastCast.VisualizeCasts = true
local caster = FastCast.new()
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true
local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate
local function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
game:GetService("Debris"):AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
local function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
if bullet then
local bulletLength = bullet.Size.Z/2
local offset = CFrame.new(0, 0, -(length - bulletLength))
bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
end
end
local function fire(player, mousePosition, shotpart)
castParams.FilterDescendantsInstances = {player, bulletsFolder}
print(mousePosition, shotpart)
local firePoint = shotpart
local origin = firePoint
local direction = (mousePosition - origin).Unit
caster:Fire(origin, direction, 1500, castBehavior)
local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
UntagHumanoid(hit.Parent)
TagHumanoid(hit.Parent.Humanoid, player)
character.Humanoid:TakeDamage(25)
print("hit")
ow = ow + 1
elseif hit and hit.Parent:IsA("Accessory") and then
hit.Parent.Parent.Humanoid:TakeDamage(25)
UntagHumanoid(hit.Parent)
TagHumanoid(hit.Parent.Parent.Humanoid, hit)
end
game:GetService("Debris"):AddItem(bullet, 0.1)
end
caster.RayHit:Connect(onRayHit)
end
fireEvent.OnServerEvent:Connect(fire)
caster.LengthChanged:Connect(onLengthChanged)
Any help would be appreciated thanks!