Blaster refuses to work correctly in one specific game only

Demonstration of the issue

What’s strange is that inside the game where the blaster refuses to work correctly (which also happens to be the only published place I have), I have zero scripts running which can possibly alter things like mouse direction

What I mean by “refuses to work correctly”:

  • There is a prominent offset from where I actually click which changes according to where my character is located
  • The dot product guard works unpredictably and also changes according to my character’s location, often still allowing to shoot backwards

Here is the client-side script:

local contextActionService = game.ContextActionService

local tool = script.Parent
local handle = tool:WaitForChild"Handle"
local attachment = handle:WaitForChild"Attachment"

local player = game.Players.LocalPlayer

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character or player.CharacterAdded:Wait(), tool}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
player = nil

tool.Equipped:Connect(function()
	handle.Equip:Play()

	local workspace = workspace

	local currentCamera = workspace.CurrentCamera

	local remoteEvent = game.ReplicatedStorage.Blaster

	local function getDirection(position)
		local ray = currentCamera:ScreenPointToRay(position.X, position.Y)
		local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * 1024, raycastParams)

		return raycastResult and raycastResult.Position or ray.Origin + ray.Direction * 1024
	end

	contextActionService:BindAction("MouseButton1", function(_, inputState, inputObject)
		if inputState ~= Enum.UserInputState.Begin then return end

		local origin = attachment.WorldPosition
		local direction = (getDirection(inputObject.Position) - origin).Unit * 1024

		if direction:Dot(attachment.WorldCFrame.LookVector) < 0 then return end

		local raycastResult = workspace:Raycast(origin, direction, raycastParams)
		local distance, humanoid

		if raycastResult then
			distance = raycastResult.Distance
			humanoid = raycastResult.Instance.Parent:FindFirstChild"Humanoid"
		else
			distance = (direction - origin).Magnitude
		end

		remoteEvent:FireServer(
			handle.Activate,
			Vector3.new(0.1, 0.1, distance),
			CFrame.lookAt(origin, direction) * CFrame.new(0, 0, -distance / 2),
			humanoid)
	end, false, Enum.UserInputType.MouseButton1)
end)

tool.Unequipped:Connect(function()
	contextActionService:UnbindAction"MouseButton1"
end)
tool = nil

Note: I’m aware I’m sending sensitive data to the server, but this is a game I made for me to play with my personal friends only

The server-side script:

local debris = game.Debris

local part = script.Laser

game.ReplicatedStorage.Blaster.OnServerEvent:Connect(function(_, sound, size, cframe, humanoid)
	sound:Play()

	local part = part:Clone()
	part.Size = size
	part.CFrame = cframe
	part.Parent = workspace
	debris:AddItem(part, 1)

	if humanoid then humanoid:TakeDamage(20) end
end)

If you wish to recreate my blaster, I simply followed the creating player tools tutorial and added an attachment so that the beam originates from the barrel. The attachment’s position is: 0, 0.25, -1.75 and the orientation is zero

I also tried recreating the client script using the Mouse object with the same results. I promise that copy-pasting into a default baseplate somehow magically fixes the issues

If your issue is inaccurate mouse data being returned, try using UserInputService to get the mouse position at the instant the function is called? If your issues were with other things, feel free to correct me.

I forgot to mention that I also tried UserInputService:GetMouseLocation() and remembered to switch to ViewportPointToRay, but unfortunately the same issues show up

Thank you for your suggestion though :slightly_smiling_face:

Is this issue occuring in first or third person?

Yes, it is present both in first and third person

Is there a consistent offset to the mouse? Is it possible I can see a video demonstration

The offset actually varies according to where my character is located and somehow disappears whenever I’m north of the world origin

I won’t be able to record a video today but should be able to tomorrow

Get back to me and I’d be happy to help!

1 Like

Here is the link of the video I recorded today: link

wouldn’t it be easier to just just send the origin and hit position/end of ray position to the server. and make the beam off of that. just get magnitude of the origin and hit position for size.

Since this is a (sort of) private game, I’m comfortable with pre-calculating things on the client in order to minimise load on the server

not really a lot of load but you could try this

local origin -- your origin
local mousePosition = Input.Position
local ray = currentCamera:ScreenPointToRay(position.X, position.Y)
local rayLength = 1024

local result = workspace.Raycast(ray.Origin, ray.Direction * rayLength, raycastParams)
local hitPosition = result and result.Position or ray.Origin + ray.Direction * rayLength

local size = (origin - hitPosition).Magnitude

local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, size)
part.Cframe = Cframe.new(origin:Lerp(hitPosition, 0.5), hitPosition)

this is how I would do it. not tested.

It does seem to fix the offset issue, but it’s causing the dot product check to malfunction

this doesnt work? I havent used dot in a long time.

if (hitPosition.Unit * rayLength):Dot(attachment.WorldCFrame.LookVector) < 0 then return end

I edited it to match your example and it’s still not working unfortunately. As a matter of fact the gun now almost always refuses to fire

Edit: That’s weird it works if I’m north of the world origin
Edit: Sorry I was wrong as it’s allowing me to shoot backwards

does this work? I just looked up what dot does so hopefully it works.

local dir = (origin - hitPosition).Unit
if (dir):Dot(attachment.WorldCFrame.LookVector) < 0 then return end
1 Like

just filp the < to a > then it should work fine.

1 Like

It’s working, thank you! By the way, do you also know a good way as to how to limit the beam’s distance? :slightly_smiling_face:

the rayLength is how far it will go

Personally I found setting the rayLength to values below 128 causes inaccuracies

Edit: Below 16 actually