i have this really big issue that just breaks my entire code and i don’t know why
it’s quite a long piece of code so please bear with me
right anyway, i’ll send the video first:
yup, upon switching the guns, the origin and sound doesn’t change, but everything else seems to work (For now)
i’m gonna be honest i have no idea what to do i’ve been writing this for half the day i’m tired
local ShootEvent = game:GetService("ReplicatedStorage"):WaitForChild("GunEvents").ShootEvent
local UpdateHitPosEvent = game:GetService("ReplicatedStorage"):WaitForChild("GunEvents").UpdateHitPosEvent
local DamageIndicatorEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlayerEvents").DamageGUI
local gunInfo = require(game:GetService("ServerScriptService"):WaitForChild("Weapons"):WaitForChild("Gun").GunStats)
local dService = game:GetService("Debris")
local players = game:GetService("Players")
local gunTable = {}
ShootEvent.OnServerEvent:Connect(function(player, initialHitPos)
local tool = player.Character:FindFirstChildOfClass("Tool")
gunTable[player.UserId] = gunTable[player.UserId] or {
gun = tool,
bullets = tool.Bullets.Value,
gunType = tool.GunType.Value,
previousTime = tick()
}
local data = gunTable[player.UserId]
-- // YOINKED FROM CHAT GPT
local hitPos = initialHitPos
-- Event to update hitPos
local updateHitPosConnection = UpdateHitPosEvent.OnServerEvent:Connect(function(updatePlayer, newHitPos)
if updatePlayer == player then
hitPos = newHitPos
end
end)
-- // END OF YOINKED
-- // AUTOMATICS
if tool.GunType.Value == "AUTOMATIC" then
local gun = data["gun"]
local spread = gun.Spread
local flashPart = gun.FlashPart
local shootSound = flashPart.ShootSound
local muzzleFlash = flashPart.Light
local flashImage = flashPart.FlashImage
local HEADSHOT_DAMAGE = gunInfo[tool.Name]["HEADSHOT_DAMAGE"]
local BODYSHOT_DAMAGE = gunInfo[tool.Name]["BULLET_DAMAGE"]
local FIRERATE = gunInfo[tool.Name]["FIRERATE"]
if data.bullets < 1 then player:Kick("Your manipulation techniques are atrocious.") return end
if data.bullets > gunInfo[tool.Name]["MAX_BULLETS"] then player:Kick("The underworld calls to you.") return end
gun.IsHolding.Value = true
while gun.IsHolding.Value and data.bullets > 0 do
if data.bullets > gunInfo[tool.Name]["MAX_BULLETS"] then player:Kick("The underworld calls to you.") end
tool.Bullets.Value -= 1
shootSound:Play()
coroutine.wrap(function() -- coroutine to prevent yielding
muzzleFlash.Enabled = true
flashImage.Enabled = true
task.wait(0.1)
muzzleFlash.Enabled = false
flashImage.Enabled = false
end)()
-- // RAYCASTING
local origin = flashPart.Position -- please forgive me for this variable i'm sorry
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local direction = (hitPos - origin).Unit
local displacement = direction * gunInfo[tool.Name]["MAX_DISTANCE"]
local result = workspace:Raycast(
origin,
displacement,
raycastParams
)
local endPos = nil
if result then
endPos = result.Position
local character = result.Instance.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid ~= players:GetPlayerFromCharacter(character) then
if result.Instance == character.Head then
humanoid:TakeDamage(HEADSHOT_DAMAGE)
else
humanoid:TakeDamage(BODYSHOT_DAMAGE)
end
end
else
endPos = origin + displacement
end
-- // TRACERS
local tracer = Instance.new("Part")
tracer.Parent = workspace
tracer.Anchored = true
tracer.CanCollide = false
tracer.CanQuery = false
tracer.Transparency = 0.6
tracer.Color = Color3.new(0.960784, 0.666667, 0.156863)
dService:AddItem(tracer, 0.2)
local tracerLength = (endPos - origin).Magnitude
tracer.Size = Vector3.new(0.07, 0.07, tracerLength)
tracer.CFrame = CFrame.lookAt(origin + (endPos - origin) / 2, endPos)
task.wait(FIRERATE)
end
updateHitPosConnection:Disconnect()
---------------------------------------------------------------------------------------------------------------------------------------------------
-- // SEMIS
elseif tool.GunType.Value == "SEMI" then
local gun = data["gun"]
local spread = gun.Spread
local flashPart = gun.FlashPart
local shootSound = flashPart.ShootSound
local muzzleFlash = flashPart.Light
local flashImage = flashPart.FlashImage
local BODYSHOT_DAMAGE = gunInfo[tool.Name]["BULLET_DAMAGE"]
local HEADSHOT_DAMAGE = gunInfo[tool.Name]["HEADSHOT_DAMAGE"]
local FIRERATE = gunInfo[tool.Name]["FIRERATE"]
if tool.Shooting.Value then return end
if tool.Bullets.Value < 1 then player:Kick("Your manipulation techniques are atrocious.") return end
if tool.Bullets.Value > gunInfo[tool.Name]["MAX_BULLETS"] then player:Kick("The underworld calls to you.") return end
tool.Shooting.Value = true
shootSound:Play()
coroutine.wrap(function() -- coroutine to prevent yielding
muzzleFlash.Enabled = true
flashImage.Enabled = true
task.wait(0.1)
muzzleFlash.Enabled = false
flashImage.Enabled = false
end)()
local origin = flashPart.Position -- please forgive me for this variable i'm sorry
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local direction = (hitPos - origin).Unit
local displacement = direction * gunInfo[tool.Name]["MAX_DISTANCE"]
local result = workspace:Raycast(
origin,
displacement,
raycastParams
)
local endPos = nil
if result then
endPos = result.Position
local character = result.Instance.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid ~= players:GetPlayerFromCharacter(character) then
if result.Instance == character.Head then
humanoid:TakeDamage(HEADSHOT_DAMAGE)
else
humanoid:TakeDamage(BODYSHOT_DAMAGE)
end
end
else
endPos = origin + displacement
end
-- // TRACERS
local tracer = Instance.new("Part")
tracer.Parent = workspace
tracer.Anchored = true
tracer.CanCollide = false
tracer.CanQuery = false
tracer.Transparency = 0.6
tracer.Color = Color3.new(0.960784, 0.666667, 0.156863)
dService:AddItem(tracer, 0.2)
local tracerLength = (endPos - origin).Magnitude
tracer.Size = Vector3.new(0.07, 0.07, tracerLength)
tracer.CFrame = CFrame.lookAt(origin + (endPos - origin) / 2, endPos)
task.wait(FIRERATE)
tool.Shooting.Value = false
end
end)