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