Reticle position is out of bounds

I am trying to make it so i can see the reticle through a scope but I want it to have a sway. Problem is the position is random


local cam = game.Workspace.CurrentCamera
local runservice = game:GetService("RunService")

cam.ChildAdded:Connect(function()
	if cam:FindFirstChild("MCX-Victus") then
		for index, Part in pairs(cam:FindFirstChild("MCX-Victus"):GetDescendants()) do
			if Part:IsA("BasePart") and Part.Name == "SightMark" then
				local dist_scale = Part.CFrame:pointToObjectSpace(cam.CFrame.Position) / Part.Size
				local reticle = Part.SurfaceGui.Border.Scope	
				reticle.Position = UDim2.new(0.5 + dist_scale.x, 0, 0.5 - dist_scale.y, 0)	
				print("dist_scale:", dist_scale)
				print("reticle.Position:", reticle.Position)
				
				if Part.SurfaceGui.Border:FindFirstChild("Vignette") then
					--Part.SurfaceGui.Border.Vignette.Position = reticle.Position
				end
			end
		end
	end
end)


Can anyone help?

This is related to the RunService’s “Heartbeat” event, which runs every frame update.

I made a revised script that should help you:

local cam = game.Workspace.CurrentCamera
local runservice = game:GetService("RunService")

runservice.Heartbeat:Connect(function()
    local obj = cam:FindFirstChild("MCX-Victus")
    if obj then
        for index, Part in pairs(obj:GetDescendants()) do
            if Part:IsA("BasePart") and Part.Name == "SightMark" then
                local dist_scale = Part.CFrame:pointToObjectSpace(cam.CFrame.Position) / Part.Size
                local reticle = Part.SurfaceGui.Border.Scope
                reticle.Position = UDim2.new(0.5 + dist_scale.x, 0, 0.5 - dist_scale.y, 0)	
                print("dist_scale:", dist_scale)
                print("reticle.Position:", reticle.Position)
                
                if Part.SurfaceGui.Border:FindFirstChild("Vignette") then
                    --Part.SurfaceGui.Border.Vignette.Position = reticle.Position
                end
            end
        end
    end
end)

I made it so runservice.Heartbeat:Connect loop runs until the MCX-Victus object is found, and as it is controlled by the heartbeat service, it updates every frame, instead of only once. This should help in creating the sway effect as the position of the reticle is updating constantly, and its new position is determined by the current position of the camera.

Also make sure the MCX-Victus object has a descendent with name SightMark that has SurfaceGui.Border.Scope as a descendant, and the scope is located correctly inside a SurfaceGui in the game.

2 Likes

4 month old reply, I found a fix a long time ago but thanks for replying with a detailed solution.

1 Like

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