You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to fix my mouse position finding script -
What is the issue? Include screenshots / videos if possible!
The script lags the game a lot when the target is nil and spams errors, It seems to still do stuff when the mouse isnt moving but only seems to break when the target is the sky then it will create errors until I close the server -
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I tried a system where the mouse only updates if it is moved and every half a second
--local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local target = Instance.new("Part")
local UserInputService = game:GetService("UserInputService")
target.Parent = game.Workspace
target.CanQuery = false
target.CanCollide = false
target.Transparency = 0.6
target.Size = Vector3.new(1,1,1)
target.Anchored = true
local event = game.ReplicatedStorage.TargetPosition
local Time = os.time()
local function OnMouseMove(pos)
if os.time() - Time < 1 then return end --Ignore if the event was fired in the last 1 seconds.
Time = os.time()
event:FireServer(pos)-- *
end
game:GetService("RunService").Heartbeat:Connect(function()
if not game:IsLoaded() then return end
local mousePosition = UserInputService:GetMouseLocation()
local ray = workspace.CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = player.Character:GetDescendants()
local result = workspace:Raycast(ray.Origin, ray.Direction * 200, raycastParams)
mouse.Move:Connect(function()
if result.Position ~= nil then
OnMouseMove(result.Position)
end
end)
end)
*(By the way the server script just prints the output at this line and nothing else)