I’m trying to get the gun tool I made to have a line that goes from the gun to the target.
Reference:
I’m not sure how to get this to work without raycasting
I tried finding a tutorial for my scenario.
Here’s the server-sided code (client sided code is literally just detecting the target of the click)
local Players = game:GetService("Players")
local Friendly = script.Parent:FindFirstChild("Friendly").Value
local Damage = script.Parent:FindFirstChild("Damage").Value
local Sound = script.Parent.Pistol.Pistol.Fired
script.Parent.Activation.OnServerEvent:Connect(function(plr, target)
Sound:Play()
if Players:GetPlayerFromCharacter(target.Parent) ~= nil and Friendly == true then return end
if target:IsDescendantOf(plr.Character) then
print("Can't damage own player")
else
if target.Parent:FindFirstChildWhichIsA("Humanoid") then
if target.Name == "Head" then
target.Parent.Humanoid.Health -= Damage * 2
else
target.Parent.Humanoid.Health -= Damage
end
end
end
end)
It isn’t using raycast, although if anyone can change the gun to be raycast based then it would work, but right now it’s just using the target of the mouse click.
Well if it have an origin and the final position then you can still use it.
replace result.Position = your mouse.Hit
origin = where you fire
distance = (origin-position).Magnitude
local result = workspace:Raycast(EndPart)
if result then
local distance = (EndPart.Position - target.Position).Magnitude
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.lookAt(EndPart, target.Position)*CFrame.new(0, 0, -distance/2)
end