I’ve been trying to change my gun script as some one said that the current one is what has been causing my raycast lag. It originally was one local script that did everything but I changed it so the client raycasts from gun and then tells the server if it has hit something so the server hurts the hit thing.
But now that I changed it the ray keeps detecting the player instead of the enemy. So it harms the player. The thing that has been confusing me is that it only harms the player if the ray hits an enemy. And If I shoot a random part it nothing happens as I have missed.
I’ve tried to use a ignore list, but I doubt I have been doing it wrong. The problem may also be in the remote event params. I have only been coding in Lua for about 2 months so i’m not too good
Any help is appreciated!
Client Script
--Variables---
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local firePart = script.Parent:WaitForChild("firePart")
local canShoot = true
local UserInputService = game:GetService("UserInputService")
local reloading = false
local light = script.Parent.firePart.SpotLight
local repStorage = game:GetService("ReplicatedStorage")
local fireGunEvent = repStorage.Events.Shot
--Sounds--
local shotSound = script:WaitForChild("Shot")
local reloadSound = script.Reloading
--GunSettings--
local fireRate = .1
local dmg = 20
local critDmg = 30
local magSize = script.magSize
local ammoCount = script.AmmoCount
local reloadSpeed = 2
--Shooting--
tool.Equipped:Connect(function(mouse)
print(player.Name.." equipped the Magnum!")
mouse.Button1Down:Connect(function()
if canShoot and ammoCount.Value > 0 and (reloading ~= true) then
shotSound:Play()
light.Enabled = true
ammoCount.Value = ammoCount.Value - 1
canShoot = false
local HRP = player.character:FindFirstChild("HumanoidRootPart")
local ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, true, false)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("New Yeller")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (firePart.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(firePart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
wait(fireRate)
canShoot = true
light.Enabled = false
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
fireGunEvent:FireServer(humanoid)
end
end
end
end)
--reload--
UserInputService.InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.R and ammoCount.Value < magSize.Value and (reloading ~= true) then
local reloadNoti = player.PlayerGui.gunGui.Frame.reload
reloading = true
reloadNoti.Text = "RELOADING..."
reloadSound:Play()
wait(reloadSpeed)
ammoCount.Value = magSize.Value
wait()
reloadNoti.Text = ""
reloading = false
end
end)
end)
Server Script
local repStorage = game:GetService("ReplicatedStorage")
local fireGunEvent = repStorage.Events.Shot
local hitSound = script.Hit
local critSound = script.Crit
local critDmg = 30
local dmg = 20
fireGunEvent.OnServerEvent:Connect(function(humanoid)
print("works")
print(humanoid.Name)
humanoid:TakeDamage(dmg)
hitSound:Play()
end)