i am trying to make the player’s gun move back when fired, but it goes to the left when i turn, it works when my character look at the right direction, heres the script:
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local GunDamage = 30
LaserGun.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.Position = LaserGun.Position + Vector3.new(-0.1,0,0)
local LaserBeam = Instance.new("Part", game.Workspace)
LaserBeam.BrickColor = BrickColor.new("Bright green")
LaserBeam.FormFactor = "Custom"
LaserBeam.Material = "Neon"
LaserBeam.Transparency = 0.25
LaserBeam.Anchored = true
LaserBeam.CanCollide = false
local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
Debris:AddItem(LaserBeam, 0.1)
if HitPart then
local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")
if not HitHumanoid then
HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")
wait(0.1)
LaserGun.Position = LaserGun.Position + Vector3.new(0.1,0,0)
end
if HitHumanoid and HitHumanoid ~= Character.Humanoid then
HitHumanoid:TakeDamage(GunDamage)
wait(0.1)
LaserGun.Position = LaserGun.Position + Vector3.new(0.1,0,0)
end
end
end)
end)
LaserGun.Position = LaserGun.Position + Vector3.new(-0.1,0,0)
I’m assuming that’s the code that moves the gun. The problem with this is that you’re only moving it on the X axis. If you want the part to move in a relative direction, you’ll need to use either a LookVector, UpVector, or RightVector. See here or here
hmm like this? oh it moves the character instead of the tool ;-;
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local GunDamage = 30
LaserGun.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.CFrame = Tip.CFrame * CFrame.new(-1, 0, 0)
local LaserBeam = Instance.new("Part", game.Workspace)
LaserBeam.BrickColor = BrickColor.new("Bright green")
LaserBeam.FormFactor = "Custom"
LaserBeam.Material = "Neon"
LaserBeam.Transparency = 0.25
LaserBeam.Anchored = true
LaserBeam.CanCollide = false
local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
Debris:AddItem(LaserBeam, 0.1)
if HitPart then
local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")
if not HitHumanoid then
HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")
wait(0.1)
LaserGun.Position = LaserGun.Position + Vector3.new(0.1,0,0)
end
if HitHumanoid and HitHumanoid ~= Character.Humanoid then
HitHumanoid:TakeDamage(GunDamage)
wait(0.1)
LaserGun.Position = LaserGun.Position + Vector3.new(0.1,0,0)
end
end
end)
end)
not used to working with cframes
LaserGun.Position += LaserGun.CFrame.LookVector * -1;
LaserGun.Position += LaserGun.CFrame.UpVector * -1;
LaserGun.Position += LaserGun.CFrame.RightVector * -1;
I don’t know what the directional vectors of LaserGun
are, so you’ll have to plug and play with one of these.
well heres the position of the mesh
-0.39, 4.48, 4.97-- for rotation
-402.9, -29.002, -65.8 -- for position
Directional vectors are a special type of vector. You can’t see them in the properties menu cuz it’s a property of a part’s CFrame, which is hidden in the properties menu.
the lookvector one is moving the gun sideways, the upvector is going downwards annnnnddd the rightvecter worked pog
1 Like
O noo it seems like when i spam it it extendes more and not turning back, how would i fix that? i tried setting the position to the oringinal position but that made it worse
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local GunDamage = 30
LaserGun.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
local LaserBeam = Instance.new("Part", game.Workspace)
LaserBeam.BrickColor = BrickColor.new("Bright green")
LaserBeam.FormFactor = "Custom"
LaserBeam.Material = "Neon"
LaserBeam.Transparency = 0.25
LaserBeam.Anchored = true
LaserBeam.CanCollide = false
local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
Debris:AddItem(LaserBeam, 0.1)
if HitPart then
local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")
if not HitHumanoid then
HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")
wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
end
if HitHumanoid and HitHumanoid ~= Character.Humanoid then
HitHumanoid:TakeDamage(GunDamage)
wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
end
end
wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
end)
end)