I want to make a gun system.
The raycasting is weird, you have to aim infront of someone to make a hit.
I don’t know how I can make it faster, someone help that might
https://gyazo.com/7390cd875625618d9f03cc2194a192cf
I really need help and it would be appreciated
Local script
function OnActivated(ActivatedToolYesOk)
local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()
local FireTool = script.Parent
local PlayeMousIcon = Player:GetMouse()
if Ammo >= 1 then
if CurrentReloading == false then
wait()
Ammo = Ammo - 1
bullet = CreateBullet(PlayerMouse.Hit.Position)
local shakeRate = 0 --how often to shake the screen in seconds. 0 is default, around 0.0166 seconds.
local shakeMin = 0 --the minimum amount of shake to apply, use whole numbers only
local shakeMax = 400 --the maximum amount of shake to apply, use whole numbers only
Player.Character.Humanoid.CameraOffset = Vector3.new(math.random(shakeMin, shakeMax)/1000, math.random(shakeMin, shakeMax)/1000, math.random(shakeMin, shakeMax)/1000)
wait()
Player.Character.Humanoid.CameraOffset = Vector3.new(0,0,0)
script.Parent.PistolEvent:FireServer(PlayerMouse.Hit.Position, FireTool)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (PlayerMouse.Hit.Position - script.Parent.Handle.Position)*300,raycastParams)
if raycastResult then
local hitpart = raycastResult.Instance
local Model = hitpart:FindFirstAncestorOfClass("Model")
if Model then
if Model:FindFirstChild("Humanoid") then
if Model:FindFirstChild("Humanoid").HealthChanged then
PlayeMousIcon.Icon = "rbxassetid://7193947094"
end
end
end
wait()
PlayeMousIcon.Icon = "rbxassetid://62814275"
end
end
end
end
Server Script
-- Animations
local PullOutAnimation = script.Parent.Handle.PullOut
local GunPositioningAnimationHold = script.Parent.Handle.GunPosition
local GunPositiongAniTrack;
local PullOutAniTrack;
local Equipped = false;
script.Parent.PistolEvent.OnServerEvent:Connect(function(Player, PlayerMouse, FireTool, PlayeMousIcon)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (PlayerMouse - script.Parent.Handle.Position)*300,raycastParams)
if raycastResult then
local hitpart = raycastResult.Instance
local Model = hitpart:FindFirstAncestorOfClass("Model")
if Model then
if Model:FindFirstChild("Humanoid") then
Model.Humanoid.Health -= 50 -- Change damage if wanted
end
end
end
local FireAnimation = script.Parent.Handle.GunFire
local FireAniTrack;
FireAniTrack = Player.Character.Humanoid:LoadAnimation(FireAnimation)
FireAniTrack:Play()
FireTool:WaitForChild("Handle").Fire:Play()
script.Parent.Bolt.Attachment.ParticleEmitter.Enabled = true
script.Parent.Bolt.Attachment.SpotLight.Enabled = true
wait(0.4)
script.Parent.Bolt.Attachment.ParticleEmitter.Enabled = false
script.Parent.Bolt.Attachment.SpotLight.Enabled = false
end)