I’m trying to make a 3rd person shooter game but my gun is being strange. It isn’t shooting the right way!
Here is a video.
robloxapp-20200427-1455482.wmv (1.9 MB)
I’m not sure what’s wrong as i used basically the same script as my other guns which work fine!
Here is my code as well.
---Local Script
wait()
local tool = script.Parent.Parent
local user
local firing = false
local origin = tool.Hole
local mouse = game.Players.LocalPlayer:GetMouse()
local mousehit = mouse.Hit.p
tool.Equipped:Connect(function()
script.Parent.Parent.Loaded:Play()
end)
mouse.Button1Up:Connect(function()
firing = false
end)
mouse.Button1Down:connect(function()
firing = true
repeat
game.ReplicatedStorage.Events.Fire:FireServer(mousehit,origin)
wait(.1)
until firing == false or script.Parent.Parent.Ammo.Value == 0
end)
---Server Script
game.ReplicatedStorage.Events.Fire.OnServerEvent:Connect(function(player,mousehit,origin)
if script.Parent.Parent.Ammo.Value > 0 then
script.Parent.Parent.Ammo.Value = script.Parent.Parent.Ammo.Value - 1
local ray = Ray.new(origin.CFrame.p, (mousehit - origin.CFrame.p).unit * 300)
local hit, position = game.Workspace:FindPartOnRay(ray, player.Character)
origin.Parent.Fire:Play()
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
local distance = (position - origin.CFrame.p).magnitude
local rayPart = Instance.new("Part", player.Character)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("New Yeller")
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.Material = Enum.Material.Plastic
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance - 6)
rayPart.CFrame = CFrame.new(position, origin.CFrame.p) * CFrame.new(0,0,-distance/2)
game.Debris:AddItem(rayPart, 1/30)
script.Parent.Parent.Hole.Smoke.Enabled = true
wait(.1)
script.Parent.Parent.Hole.Smoke.Enabled = false
end
end)
it also comes back with some errors sometimes saying things like “Hit is a nil value”