Attachment Placed Wrong

Hello, I’m trying to make a beam for my gun, but the beam goes in the wrong direction. I can send a video if needed, but just know it goes in the wrong direction
Script:

local Modules = game:GetService("ServerStorage"):WaitForChild("Server_Modules")
local GunSystem = require(Modules.GunSystem)

local Events = game:GetService("ReplicatedStorage"):WaitForChild("Events")
local ActivateEvent = Events:WaitForChild("GunEvent")

local debounce = {}

local function createBeam(handle, endPos, lifetime)
	local attach0 = Instance.new("Attachment")
	attach0.Position = handle:WaitForChild("Origin").Position
	attach0.Parent = handle
	
	local attach1 = Instance.new("Attachment")
	attach1.WorldPosition = endPos
	attach1.Parent = handle
	
	local beam = Instance.new("Beam")
	beam.Attachment0 = attach0
	beam.Attachment1 = attach1
	beam.FaceCamera = true
	beam.Width0 = 0.1
	beam.Width1 = 0.1
	
	local debris = game:GetService("Debris")
	--[[debris:AddItem(attach0, lifetime)
	debris:AddItem(attach1, lifetime)
	debris:AddItem(beam, lifetime)]]
	
	beam.Parent = workspace.Effects
end

local function raycast(character, origin, direction, sound)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	
	if raycastResult then
		if raycastResult.Instance:FindFirstAncestorWhichIsA("Model") then
			local character = raycastResult.Instance:FindFirstAncestorWhichIsA("Model")
			if character:FindFirstChild("Humanoid") then
				character.Humanoid.Health = 0
				--sound:Play()
			end
		end
		
		return raycastResult.Position
	else
		return direction
	end
end

local function onEvent(player, mousePos, origin, effects)
	local character = player.Character	
	
	if not debounce[player] then
		debounce[player] = true
		
				
		local direction = (mousePos - origin).Unit * 100
		local hit = GunSystem.wallcheck(character:WaitForChild("Head"))
				
		if hit then
			--origin = direction * -114
		end
		
		local endPos = raycast(character, origin, direction, effects["Sounds"].Death)	
		--ActivateEvent:FireAllClients(endPos, 0.1)
		createBeam(effects.Parent.Parent:WaitForChild("Handle"), endPos, 5)
		effects["Sounds"].Fire:Play()
		
		task.wait(0.5)
		debounce[player] = false
	end
end

ActivateEvent.OnServerEvent:Connect(onEvent)

Thank you.

We need more information.

We can see the beam is supposed to go from to the tool handle attach0 to the endPos, but where is the endPos being calculated to?
In your creatBeam function you have:
attach1.WorldPosition = endPos

but in your onEvent function you have:
endPos = raycast(character, origin, direction, effects["Sounds"].Death)
which isn’t a Position.

1 Like

It’s right here thanks for asking.

Edit: what do you mean it isn’t a position?

I’m not a pro, but if you have

endPos = raycast(character, origin, direction, effects["Sounds"].Death) 
attach1.WorldPosition = endPos

It seems like endPos isn’t just the Position.

Try:
endPos = raycast(character, origin, direction, effects["Sounds"].Death)
print(endPos)
to see exactly what it contains.

1 Like

Sorry for the late response, but here’s what it printed:
2.3365442752838135, 0, -37.64241409301758
Edit: After looking through the properties of the attachment I noticed attach1 doesn’t move to the world position. Even though it’s suppose to.

Edit: I found a solution, but when I shoot the sky it’s offset.

1 Like

Ah, try searching aiming raycast at sky. I did and saw a few posts with Solutions.

1 Like

I followed this post:

and did:
origin + direction, but there’s still a slight offset.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.