I have created a ray cast gun using the laser gun wiki and added reload functions, animations, ect. But recently I noticed that the enemies would be stuck on 0 or lower health and not dying. I searched it up and one thing said that the lag could be from the fact that it is all client sided and the server doesn't realize the zombie is dead. So I created a new script using remote events, a local script, and a server script, but when I try to shoot it the ray detects the player even though I set it to ignore the player. Please tell me what I did wrong and if I'm using remote events right, just started coding about 2 months ago. The local script is creating the ray and beam is created but no damage is dealt.
Local 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 reloadNoti = player.PlayerGui.gunGui.Frame.reload local character = player.Character local HRP = character.HumanoidRootPart local light = script.Parent.firePart.SpotLight local ReplicatedStorage = game:GetService("ReplicatedStorage") local shotEvent = ReplicatedStorage.Events:WaitForChild("Shot") local ignoreList = {character} --Sounds-- local hitSound = script.Hit local critSound = script.Crit local shotSound = script.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 ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300) local part, position = workspace:FindPartOnRay(ray, character, false, true) 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 playerH = game.Players.LocalPlayer.Character.Humanoid local hit = part.Parent:FindFirstChild("Humanoid") if not hit then hit = part.Parent.Parent:FindFirstChild("Humanoid") end if hit then shotEvent:FireServer(hit) 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 reloading = true reloadNoti.Text = "RELOADING..." reloadSound:Play() wait(reloadSpeed) ammoCount.Value = magSize.Value wait() reloadNoti.Text = "" reloading = false end end) end)
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local hitSound = script.Hit local critSound = script.Crit local shotSound = script.Shot local reloadSound = script.Reloading local shotEvent = ReplicatedStorage.Events:WaitForChild("Shot") local function hasBeenShot(hit) local rand = math.random(4) if rand == 4 then critSound:Play() print("hit.Name") print("critical") hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 100 else hitSound:Play() print(hit.Name) hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 100 end end shotEvent.OnServerEvent:Connect(hasBeenShot)
Working old local 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 reloadNoti = player.PlayerGui.gunGui.Frame.reload local character = player.Character local HRP = character.HumanoidRootPart local light = script.Parent.firePart.SpotLight --Sounds-- local hitSound = script.Hit local critSound = script.Crit local shotSound = script.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 ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300) local part, position = workspace:FindPartOnRay(ray, player.Character, false, true) 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 local rand = math.random(4) if rand == 4 then humanoid:TakeDamage(critDmg) critSound:Play() print("crit") else humanoid:TakeDamage(dmg) hitSound:Play() end 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 reloading = true reloadNoti.Text = "RELOADING..." reloadSound:Play() wait(reloadSpeed) ammoCount.Value = magSize.Value wait() reloadNoti.Text = "" reloading = false end end) end)