Hi! I have this gun and it works pretty good, the only problem is, when i walk to the left or to the right, the ray doesnt start at the muzzle anymore… how would i fix this?
Here’s the script:
Server Script:
game.ReplicatedStorage.Events.Shoot.OnServerEvent:Connect(function(plr, origin, direction)
local char = plr.Character
local rayparams = RaycastParams.new()
rayparams.FilterDescendantsInstances = {char.RightHand}
rayparams.FilterType = Enum.RaycastFilterType.Blacklist
local radius = .1
local result = Ray.new(origin, direction)
local midpoint = result.Origin + result.Direction /2
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.BrickColor = BrickColor.new("New Yeller")
part.Material = Enum.Material.Neon
part.CFrame = CFrame.new(midpoint, result.Origin)
part.Size = Vector3.new(radius, radius, result.Direction.magnitude)
local light = Instance.new("PointLight")
light.Color = Color3.new(1, 1, 0)
light.Parent = plr.Character:FindFirstChildOfClass("Tool").Parts.Flame
wait(.01)
part:Destroy()
wait(.03)
light:Destroy()
end)
Client Script:
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local gun = script.Parent.Parent
local isdown = false
local isequipped = false
gun.Equipped:Connect(function()
isequipped = true
local character = plr.Character
character.Humanoid:LoadAnimation(script.Idle):Play()
end)
gun.Unequipped:Connect(function()
isequipped = false
local character = plr.Character
character.Humanoid:LoadAnimation(script.Idle):Stop()
end)
mouse.Button1Down:Connect(function()
isdown = true
end)
mouse.Button1Up:Connect(function()
isdown = false
end)
while true do
wait()
if isdown == true and isequipped == true then
wait(.1)
local origin = gun.Parts.Flame
game.ReplicatedStorage.Events.Shoot:FireServer(origin.Position, origin.CFrame.LookVector * 50)
end
end
Thanks for taking a look!
Edit:
This is what im trying to achieve: