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

I can't figure out what is wrong with this script?

Asked by
novipak 70
9 years ago

Mmk, so I got this script to work, just not the way I want it to. Basically, when you hover over an NPC, a grey SelectionBox will appear around it, and if you click on it, a red SelectionBox will appear around it. However, when you already have a red SelectionBox around an NPC and you scroll over a second NPC, the grey SelectionBox goes over it fine, however clicking on it does make the red SelectionBox appear. If you see any variables that are not used, that's because I plan to expand this script later on. Anyways, here is all the code involved with it:

Setting Player values:

game.Players.PlayerAdded:connect(function(plr)
    local character = plr.Character
    if not character or character.Parent == nil then -- what for character
        character = plr.CharacterAdded:wait()
    end
    local val = Instance.new("BoolValue",character) -- set all variables needed
    val.Name = "player"
    local val2 = Instance.new("BoolValue",character)
    val2.Name = "combat"
    local val3 = Instance.new("BoolValue",character)
    val3.Name = "talking"
    local val4 = Instance.new("BoolValue",character)
    val4.Name = "reading"
end)

mouse script:

local player = game.Players.LocalPlayer
local character = player.Character

local mouse = player:GetMouse()

if not character or character.Parent == nil then
    character = player.CharacterAdded:wait()
end

local selection = Instance.new("SelectionBox", player.PlayerGui)
selection.Color = BrickColor.new("Medium stone grey")

local cmbselection = Instance.new("SelectionBox", player.PlayerGui)
cmbselection.Color = BrickColor.new("Bright red")

local talkselection = Instance.new("SelectionBox", player.PlayerGui)
talkselection.Color = BrickColor.new("Bright green")

local bookselection = Instance.new("SelectionBox", player.PlayerGui)
bookselection.Color = BrickColor.new("Bright blue")

mouse.Move:connect(function()
    mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
    local target = mouse.Target
    local combat = character.combat
    local plr = character.player
    local talking = character.talking
    local reading = character.reading
    if target and target.Parent:IsA("Model")then
        local hum = target.Parent:FindFirstChild("Humanoid")
        local p = target.Parent:FindFirstChild("player")
        local enemy = target.Parent:FindFirstChild("enemy")
        local friend = target.Parent:FindFirstChild("friend")
        local book = target.Parent:FindFirstChild("book")
        if hum and enemy and p == nil and combat.Value == false then
            selection.Adornee = target.Parent
            local targetpos = target.Parent.Torso.Position
            local distance = player:DistanceFromCharacter(targetpos)
            mouse.Icon = "http://www.roblox.com/asset/?id=165194824"
            mouse.Button1Down:connect(function()
                if distance < 40 and combat.Value == false then
                    combat.Value = true
                    selection.Adornee = nil
                    cmbselection.Adornee = target.Parent
                end
            end)
        elseif hum and enemy and p == nil and combat.Value == true and cmbselection.Adornee ~= target.Parent then
            local targetpos = target.Parent.Torso.Position
            local distance = player:DistanceFromCharacter(targetpos)
            selection.Adornee = target.Parent
            mouse.Icon = "http://www.roblox.com/asset/?id=165194824"
            mouse.Button1Down:connect(function()
                if distance < 40 and combat.Value == true then
                    selection.Adornee = nil
                    cmbselection.Adornee = target.Parent
                end
            end)
        elseif p or hum == nil or enemy == nil or friend == nil or book == nil then
            mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
            selection.Adornee = nil
        elseif p~= nil and hum and enemy and cmbselection.Adornee == target.Parent then
            mouse.Icon = "http://www.roblox.com/asset/?id=165194824"
        end
    else 
        mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
        selection.Adornee = nil
    end
end)

I apologize if it's sloppy or inefficient, I just can't figure out this problem right now, I'd really appreciate it if you could help.

Answer this question