Hey there, I was using this tutorial but i seem to have a problem. When ever I move my mouse to the side of my screen, it shoots correctly, but the bullets dont delete and keep going. Aswell as I dont see the bullet when i shoot normally (aka in the middle of my screen or first person) unless I get very close to a dummy. Heres my code:
LOCAL SCRIPT:
local GunModel = game.ReplicatedStorage:WaitForChild(“Apple_Pistol”) – your gun
local Viewmodel = game.ReplicatedStorage:WaitForChild(“Viewmodel”) – the viewmodel!!
local AnimationsFolder = game.ReplicatedStorage:WaitForChild(“ApplePistol_Animations”)
– our main module
local MainModule = require(game.ReplicatedStorage.MainModule)
local SpringModule = require(game.ReplicatedStorage.SpringModule)
Viewmodel.Parent = game.Workspace.Camera
MainModule.weldgun(GunModel)
local RecoilSpring = SpringModule.new()
local BobbleSpring = SpringModule.new()
local SwayingSpring = SpringModule.new()
game:GetService(“RunService”).RenderStepped:Connect(function(dt)
MainModule.update(Viewmodel, dt, RecoilSpring, BobbleSpring, SwayingSpring, GunModel)
end)
MainModule.equip(Viewmodel, GunModel, AnimationsFolder[“Hold(Right)”])
local IsPlayerHoldingMouse
local CanFire = true
local Delay = 0.1
local Damage = game.ReplicatedStorage.Damage
local Fire = game.ReplicatedStorage.Fire
Fire.OnClientEvent:Connect(function(client, origin, endposition)
if client ~= game.Players.LocalPlayer then
MainModule.cast(origin, endposition, 5, nil)
end
end)
game:GetService(“RunService”).Heartbeat:Connect(function(dt)
if IsPlayerHoldingMouse then
if CanFire then
CanFire = false
RecoilSpring:shove(Vector3.new(0, 0, 10)) -- Vector3.new(up, sideways, shake)
coroutine.wrap(function()
for i, v in pairs(GunModel.Parts.Barrel:GetChildren()) do
if v:IsA("ParticleEmitter") then
v:Emit()
end
end
local FireSound = GunModel.Parts.Sounds.Fire:Clone()
FireSound.Parent = game.Workspace
FireSound.Parent = nil
FireSound:Destroy()
end)()
coroutine.wrap(function()
wait(0.2)
RecoilSpring:shove(Vector3.new(0, 0, -10))
end)()
local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.FilterDescendantsInstances = {Viewmodel, game.Players.LocalPlayer.Character}
local Mouse = MainModule.GetMouse(1000, CastParams)
MainModule.cast(GunModel.Parts.Barrel.Position, Mouse, 5, Damage)
Fire:FireServer(GunModel.Parts.Barrel.Position, Mouse)
wait(Delay)
CanFire = true
end
end
end)
game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = true
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
MainModule.aim(true, Viewmodel, GunModel)
end
end)
game:GetService(“UserInputService”).InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = false
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
MainModule.aim(false, Viewmodel, GunModel)
end
end)
MODULE SCRIPT:
local module = {}
local function GetBobbing(addition)
return math.sin(tick() * addition * 1.3) * 0.5
end
function module.update(viewmodel, dt, RecoilSpring, BobbleSpring, SwayingSpring, gun)
viewmodel.HumanoidRootPart.CFrame = game.Workspace.Camera.CFrame
local Bobble = Vector3.new(GetBobbing(10),GetBobbing(5),GetBobbing(5))
local MouseDelta = game:GetService("UserInputService"):GetMouseDelta()
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
BobbleSpring:shove(Bobble / 10 * (Character.HumanoidRootPart.Velocity.Magnitude) / 10)
SwayingSpring:shove(Vector3.new(-MouseDelta.X / 500, MouseDelta.Y / 200, 0))
local UpdatedRecoilSpring = RecoilSpring:update(dt)
local UpdatedBobbleSpring = BobbleSpring:update(dt)
local UpdatedSwaySpring = SwayingSpring:update(dt)
gun.Parts.Sight.CFrame = gun.Parts.Sight.CFrame:Lerp(viewmodel.HumanoidRootPart.CFrame, game.ReplicatedStorage.Values.AimAlpha.Value)
viewmodel.HumanoidRootPart.CFrame = viewmodel.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(UpdatedBobbleSpring.Y, UpdatedBobbleSpring.X, 0))
viewmodel.HumanoidRootPart.CFrame *= CFrame.new(UpdatedSwaySpring.X, UpdatedSwaySpring.Y, 0)
viewmodel.HumanoidRootPart.CFrame *= CFrame.Angles(math.rad(UpdatedRecoilSpring.X) * 2, 0, 0)
game.Workspace.Camera.CFrame *= CFrame.Angles(math.rad(UpdatedRecoilSpring.X), math.rad(UpdatedRecoilSpring.Y), math.rad(UpdatedRecoilSpring.Z))
end
function module.weldgun(gun)
local Main = gun.Handle
for i, v in ipairs(gun:GetDescendants()) do
if v:IsA("MeshPart") and v ~= Main then
local NewMotor = Instance.new("Motor6D")
NewMotor.Name = v.Name
NewMotor.Part0 = Main
NewMotor.Part1 = v
NewMotor.C0 = NewMotor.Part0.CFrame:inverse() * NewMotor.Part1.CFrame -- Dont worry about this, it's just for repositioning
NewMotor.Parent = Main
end
end
end
function module.equip(viewmodel, gun, hold)
local GunHandle = gun.Handle
local HRP_Motor6D = viewmodel:WaitForChild(“HumanoidRootPart”).Handle
gun.Parent = viewmodel
HRP_Motor6D.Part1 = GunHandle
local Hold = viewmodel.AnimationController:LoadAnimation(hold)
Hold:Play()
end
function module.cast(origin, endposition, velocity, damage)
local Bullet = game.ReplicatedStorage.Apple:Clone()
Bullet.Size = Vector3.new(0.1, 0.1, 5)
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.Parent = game.Workspace
Bullet.CFrame = CFrame.new(origin, endposition)
local Loop
Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
local Hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector * velocity * 1.5)
if Hit then
if Hit.Instance.Parent:FindFirstChild("Humanoid") and damage ~= nil then
damage:FireServer(Hit.Instance.Parent, 10)
else
Loop:Disconnect()
Bullet:Destroy()
end
end
Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
end)
end
function module.aim(toaim, viewmodel, gun)
if toaim then
game:GetService(“TweenService”):Create(game.ReplicatedStorage.Values.AimAlpha, TweenInfo.new(1), { Value = 1 }):Play()
else
game:GetService(“TweenService”):Create(game.ReplicatedStorage.Values.AimAlpha, TweenInfo.new(1), { Value = 0 }):Play()
end
end
function module.GetMouse(Distance, CastParams)
local MouseLocation = game:GetService(“UserInputService”):GetMouseLocation()
local UnitRay = game:GetService(“Workspace”).Camera:ViewportPointToRay(MouseLocation.x, MouseLocation.y)
local origin = UnitRay.Origin
local endp = UnitRay.Direction * Distance
local Hit = game:GetService("Workspace"):Raycast(origin, endp, CastParams)
if Hit then
return Hit.Position
else
return UnitRay.Origin + UnitRay.Direction * Distance
end
end
return module
I have tried alot of things, pretty stupid it doesnt work still.