Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there an easier way to do this?

Asked by 8 years ago
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

Log in to vote
2
Answered by
4Bros 550 Moderation Voter
8 years ago

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

Ad

Answer this question