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

Is there an easier way to do this?

local mouse = game.Players.LocalPlayer:GetMouse()
local p = workspace.part
local plr = {"Head","Torso","Right Arm","Left Arm","Right Leg","Left Leg","HumanoidRootPart"}

while true do
	wait()
	if mouse.Target == nil or mouse.Target == p  then
		script.Parent.Value = ""
	elseif mouse.Target.Name == plr[1] or mouse.Target.Name == plr[2] or mouse.Target.Name == plr[3] or mouse.Target.Name == plr[4] or mouse.Target.Name == plr[5] or mouse.Target.Name == plr[6] or mouse.Target.Name == plr[7] then
		script.Parent.Value = (mouse.Target.Parent.Name)
	else
		script.Parent.Value = mouse.Target.Name
	end
end

Everything works, it just looks messy with elseif mouse.Target.Name == plr[1] or mouse.Target.Name == plr[2] or mouse.Target.Name == plr[3] or mouse.Target.Name == plr[4] or mouse.Target.Name == plr[5] or mouse.Target.Name == plr[6] or mouse.Target.Name == plr[7] then

Would there be an easier way to do that?

1 answer

✓ Accepted answer

If you wanted to optimize your code, you could just check if there is a humanoid in the target rather than checking if the target is each part of the character, but to avoid screwing up what you already have, or maybe since you want your script to do things a certain way, I'll just add on to it.

Tell me if this works since I haven't tested.

local mouse = game.Players.LocalPlayer:GetMouse()
local p = workspace.part
local plr = {"Head","Torso","Right Arm","Left Arm","Right Leg","Left Leg","HumanoidRootPart"}

function Check(target,tab)
	for i,v in next, tab do -- Search through the table
		if target.Name == v then -- If the name ==  to the value of the table
			return true 
		end
	end
	return false
end


while true do
    wait()
    if mouse.Target == nil or mouse.Target == p  then
        script.Parent.Value = ""
    elseif Check(mouse.Target,plr) then
        script.Parent.Value = (mouse.Target.Parent.Name)
    else
        script.Parent.Value = mouse.Target.Name
    end
end

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