I was working on a FPS game using a framework tutorial with this link here.
I get an error when I try to shoot, here is my current module script:
local MainModule = {}
function MainModule.Update(viewmodel, dt)
viewmodel.HumanoidRootPart.CFrame = workspace.Camera.CFrame
end
function MainModule.weldgun(gun)
local Main = gun.GunComponets.Handle
for i,v in ipairs(gun:GetDescendants()) do
if v:IsA("BasePart") 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
newMotor.Parent = Main
end
end
end
function MainModule.equip(viewmodel, gun, hold)
local GunHandle = gun.GunComponets.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 MainModule.cast(gun,endposition,velocity)
local GunBarrel = gun.GunComponets:WaitForChild("Barrel")
local Bullet = Instance.new("Part")
Bullet.Size = Vector3.new(1,1,5)
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.Color = Color3.new(255,255,255)
Bullet.Material = Enum.Material.Neon
Bullet.Parent = workspace
Bullet.CFrame = CFrame.new(Bullet.Position, endposition)
local Loop
Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
Bullet.CFrame *= CFrame.new(0,0 -velocity * (dt * 60))
if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
Bullet:Destroy()
Loop:Disconnect()
end
end)
end
return MainModule
There are 2 scripts, a local handler for the arms, and this module for all the functions, the error is in the module script, and so is the cast function. Here is the local handler because the problem might be in there(also you said so):
local GunModel = game.ReplicatedStorage:WaitForChild("SR01A1")
local AnimationsFolder = game.ReplicatedStorage:WaitForChild("SR01A1_ANIMS")
local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel")
local MainModule = require(game.ReplicatedStorage:WaitForChild("MainModule"))
ViewModel.Parent = workspace.Camera
MainModule.weldgun(GunModel)
game:GetService("RunService").RenderStepped:Connect(function(dt)
MainModule.Update(ViewModel, dt)
end)
MainModule.equip(ViewModel,GunModel,AnimationsFolder.SR01A1_ANIM)
local IsPlayerHoldingMouse
local CanFire = true
local FireDelay = 0.1
game:GetService("RunService").Heartbeat:Connect(function(dt)
if IsPlayerHoldingMouse then
if CanFire then
CanFire = false
MainModule.cast(GunModel, game.Players.LocalPlayer:GetMouse(), 60)
wait(FireDelay)
CanFire = true
end
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = true
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = false
end
end)
Ooooh I see the issue, you only just passed the Local Playerâs Mouse Object and not its actual Position lol
Try this:
local GunModel = game.ReplicatedStorage:WaitForChild("SR01A1")
local AnimationsFolder = game.ReplicatedStorage:WaitForChild("SR01A1_ANIMS")
local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel")
local Mouse = game.Players.LocalPlayer:GetMouse()
local MainModule = require(game.ReplicatedStorage:WaitForChild("MainModule"))
ViewModel.Parent = workspace.Camera
MainModule.weldgun(GunModel)
game:GetService("RunService").RenderStepped:Connect(function(dt)
MainModule.Update(ViewModel, dt)
end)
MainModule.equip(ViewModel,GunModel,AnimationsFolder.SR01A1_ANIM)
local IsPlayerHoldingMouse
local CanFire = true
local FireDelay = 0.1
game:GetService("RunService").Heartbeat:Connect(function(dt)
if IsPlayerHoldingMouse then
if CanFire then
CanFire = false
MainModule.cast(GunModel, Mouse.Hit.Position, 60)
wait(FireDelay)
CanFire = true
end
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = true
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = false
end
end)
It shoots fine, but the bullet is horrible displaced. They shoot in the direction of the mouse and angle fine, but the starting place is in the same place, not where the gun is.
I believe the first CFrame parameter will take the position of where you want the Bullet to align its position from, so you have to set that where your current Gunâs Handle is, or:
I donât think itâd matter which way you would do it, plus I donât believe CFrame.new(Pos, LookAt) isnât deprecated but rather recommend to use the new CFrame.lookAt
Alright good, but how can I do this? I could update the spawn position every frame if the position has moved? If thatâs a good idea, how could I do it?
local Loop
Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
Bullet.CFrame *= CFrame.new(0,0 -velocity * (dt * 60))
if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
Bullet:Destroy()
Loop:Disconnect()
end
end)
Isnât this what you want though? Itâll keep moving the position determined by how much Velocity youâve assigned to the Bullet, and itâll keep detecting the Position every frame using RenderStepped, and you implementing sanity checks detecting if the Part is far away enough you can destroy it & disconnect the Event