I'm making a top down game, and I want to edit my gun script so that instead of shooting at the surface my mouse points at, it shoots in the direction my mouse is moving. Here's the script:
--Variables
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character
if not character then
character = game.Players.LocalPlayer.ChildAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local tool = script.Parent
local equipped = false
local UserInputService = game:GetService("UserInputService")
local mouseDown = false
local canPlay = true
local mouse = game.Players.LocalPlayer:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("DamageEvent")
local shootSound = tool.shoot
local reloadSound = tool.Reload
local equipSound = tool.Equip
local reloadAnim = Instance.new("Animation") --Animation Setup
reloadAnim.AnimationId = "rbxassetid://3193207881"
local reloadAnimTrack = humanoid:LoadAnimation(reloadAnim)
reloadAnimTrack.Priority = 2
local idleAnim = Instance.new("Animation") --Animation Setup
idleAnim.AnimationId = "rbxassetid://4475243520"
local idleAnimTrack = humanoid:LoadAnimation(idleAnim)
idleAnimTrack.Priority = 1
idleAnimTrack.Looped = true
local ammo = tool.Ammo.Value
local gui = tool.ak47Gui
local ammoLabel = gui.Frame.ammo
local reloading = false
--Gui invisible
gui.Parent = nil
--Setting up full auto
mouse.Button1Down:Connect(function()
mouseDown = true
end)
mouse.Button1Up:Connect(function()
mouseDown = false
end)
--Setting up equipped / unequipped variables
tool.Equipped:Connect(function(mouse)
equipSound:Play()
equipped = true
gui.Parent = player.PlayerGui
script.Parent.Magazine.CanCollide = false
script.Parent.Metal.CanCollide = false
script.Parent.Select.CanCollide = false
script.Parent.Slide.CanCollide = false
script.Parent.Stock.CanCollide = false
script.Parent.Wood.CanCollide = false
script.Parent.Leather.CanCollide = false
script.Parent.Handle.CanCollide = false
script.Parent.aimPart.CanCollide = false
script.Parent.shootPart.CanCollide = false
idleAnimTrack:Play()
end)
tool.Unequipped:Connect(function(mouse)
equipped = false
gui.Parent = nil
script.Parent.Magazine.CanCollide = true
script.Parent.Metal.CanCollide = true
script.Parent.Select.CanCollide = true
script.Parent.Slide.CanCollide = true
script.Parent.Stock.CanCollide = true
script.Parent.Wood.CanCollide = true
script.Parent.Leather.CanCollide = true
script.Parent.Handle.CanCollide = true
script.Parent.aimPart.CanCollide = true
script.Parent.shootPart.CanCollide = true
idleAnimTrack:Stop()
end)
--Key Press Functions
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and equipped == true then --Reload Function
local keyPressed = input.KeyCode
if keyPressed == Enum.KeyCode.R and reloading == false then
reloading = true
canPlay = false
print("reloading!")
reloadSound:Play()
reloadAnimTrack:Play()
wait(2.5)
ammo = 30
canPlay = true
reloading = false
end
end
if input.UserInputType == Enum.UserInputType.MouseButton2 and equipped == true then --Aim Function
end
end)
--Equipped Functions
tool.Equipped:Connect(function()
while true do
if reloading == false then
ammoLabel.Text = ammo --Updating Gui Text
else
ammoLabel.Text = "Reloading" --Updating Gui Text
end
wait()
if mouseDown then --Shooting
if equipped then
if canPlay then
if ammo >= 1 then
canPlay = false
local randomnum = math.random(1.5)-1
local randomvector = Vector3.new(randomnum,randomnum,randomnum) --Spread
local ray = Ray.new(tool.shootPart.CFrame.p, (mouse.Hit.p + randomvector - tool.shootPart.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace) --Creating Ray
beam.BrickColor = BrickColor.new("Brick yellow")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.5
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.shootPart.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(tool.shootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid") --Checks for person
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid") --Checks for person
end
if humanoid then
humanoid:TakeDamage(24) --Damages
event:FireServer(humanoid)
end
end
shootSound:Play() --Play sound
ammo = ammo - 1 --Subtract ammo
wait(0.1) --Firerate
canPlay = true
elseif reloading == false then --Reload Function
reloadSound:Play()
reloadAnimTrack:Play()
reloading = true
wait(2.5)
ammo = 30
canPlay = true
reloading = false
end
end
end
end
end
end)
Thanks.
