Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to accomplish this without having to use RayCasting?

The script below creates a "SelectionBox" around any part/model that the players mouse is targeting, it doesn't however create one around the Player Model, I believe this is because mouse.Target filters out the Player Model to prevent certain things. I'm wondering if theres anyway to go about this without having to use Raycasting as I haven't yet started learning about it.

SelectionBox Script

local modelMode = false

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(0,0,5)
selection.Parent = player.PlayerGui

mouse.Move:connect(function()
    local target = mouse.Target

    if not target then
        selection.Adornee = nil
    elseif modelMode then
        if target.Parent:IsA("Model") then
            selection.Adornee = target.Parent
        elseif target.Parent.Parent:IsA("Accoutrement") then
            selection.Adornee = target.Parent.Parent
        elseif target:IsA("BasePart") then
            selection.Adornee = target
        else
            selection.Adornee = nil
        end
    else
        if target:IsA("BasePart") then
            selection.Adornee = target
        else
            selection.Adornee = nil
        end
    end
end)

I'm not asking for anyone to script this for me, rather to let me know if there are any other ways to go about doing this.

EDIT: Here's a GIF of what I'm trying to explain https://gyazo.com/43a0c4181bf7ed19129e1691ef0110a2

1 comment

0

connect() is deprecated in favour of Connect(). I'm not sure why this doesn't work, wonder if you can use print()s to discern what's being chosen when it hovers over the character.

1 answer

✓ Accepted answer

Thanks to Crazyman32 for creating a Module Script

Here the ModelScript You will need to create in the same folder as your localScript

ModuleScript


local Mouse = {}
Mouse.__index = Mouse
function Mouse.new()
	local player = game.Players.LocalPlayer
	assert(player, "Could not get player")
	local cam = game.Workspace.CurrentCamera
	local Ray = Ray.new
	local input = game:GetService("UserInputService")
		local button1 = Enum.UserInputType.MouseButton1
		local button2 = Enum.UserInputType.MouseButton2
		local button3 = Enum.UserInputType.MouseButton3
		local mouseMovement = Enum.UserInputType.MouseMovement
		local mouseWheel = Enum.UserInputType.MouseWheel
	local mouse = {}
	local mousePos, mouseDelta = Vector2.new(), Vector2.new()
	local clicking = {
		[button1] = false;
		[button2] = false;
		[button3] = false;
	}
	local function CreateEvent(eventName)
		local e = Instance.new("BindableEvent")
		mouse[eventName] = e.Event
		return function(...)
			e:Fire(...)
		end
	end
	-- Events ---------------------------------------------------------------
	local buttonDown = CreateEvent("ButtonDown")
	local buttonUp = CreateEvent("ButtonUp")
	local moved = CreateEvent("Moved")
	local scrolled = CreateEvent("Scrolled")
	-------------------------------------------------------------------------
	-- API ------------------------------------------------------------------
	function mouse:IsDown(inputType)
		return (clicking[inputType] == true)
	end
	function mouse:GetPosition()
		return mousePos, mouseDelta
	end
	function mouse:ProjectMouseRay(ignoreList)
		local pos = self:GetPosition()
		local ray = cam:ScreenPointToRay(pos.X, pos.Y, 0)
		ray = Ray(ray.Origin, (ray.Unit.Direction * 999))
		local hit, hitPos, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList or {}, true, false)
		local cframe = CFrame.new(hitPos, (hitPos + ray.Unit.Direction))
		return cframe, hit, normal
	end
	-------------------------------------------------------------------------
	-- UserInputService Setup -----------------------------------------------
	local function InputBegan(inputObject, gameProcessed)
		if (gameProcessed) then return end
		local inputType = inputObject.UserInputType
		if (inputType == button1 or inputType == button2 or inputType == button3) then
			clicking[inputType] = true
			buttonDown(inputType)
		end
	end
	local function InputChanged(inputObject, gameProcessed)
		--if (gameProcessed) then return end
		local inputType = inputObject.UserInputType
		if (inputType == mouseMovement) then
			local newMousePos = Vector2.new(inputObject.Position.X, inputObject.Position.Y)
			local delta = newMousePos - mousePos
			mousePos = newMousePos
			mouseDelta = Vector2.new(delta.X, delta.Y)
			moved(mousePos, mouseDelta)
		elseif (inputType == mouseWheel) then
			local num = inputObject.Position.Z
			scrolled(num < 0 and -1 or num > 0 and 1 or 0)
		end
	end
	local function InputEnded(inputObject, gameProcessed)
		--if (gameProcessed) then return end
		local inputType = inputObject.UserInputType
		if (inputType == button1 or inputType == button2 or inputType == button3) then
			clicking[inputType] = false
			buttonUp(inputType)
		end
	end
	input.InputBegan:connect(InputBegan)
	input.InputChanged:connect(InputChanged)
	input.InputEnded:connect(InputEnded)
	-------------------------------------------------------------------------
	return setmetatable(mouse, Mouse)
end
function Mouse:__tostring()
	local pos = self:GetPosition()
	return ("Mouse <" .. tostring(pos) .. ">")
end
return Mouse


and here is a edited version of your localScript

local mouse = require(script.Parent:WaitForChild("ModuleScript")).new() -- here we request it 

local modelMode = false

local player = game.Players.LocalPlayer


local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(0,0,5)
selection.Parent = player.PlayerGui


mouse.Moved:connect(function()
local cframeHit, target, surfaceNormal = mouse:ProjectMouseRay() -- here we use the modeleScript to get the target


    if not target then
        selection.Adornee = nil
    elseif modelMode then
        if target.Parent:IsA("Model") then
            selection.Adornee = target.Parent
        elseif target.Parent.Parent:IsA("Accoutrement") then
            selection.Adornee = target.Parent.Parent
        elseif target:IsA("BasePart") then
            selection.Adornee = target
        else
            selection.Adornee = nil
        end
    else
        if target:IsA("BasePart") then
            selection.Adornee = target
        else
            selection.Adornee = nil
        end
    end
end)

I hope this will solve your problem :D And no there is no other way to solve this without using ray casting

This discussion is preserved as it was. New answers, comments, and votes are closed.