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

How would i make improve this script so that it will run when a model is added to workspace?

Asked by 10 years ago

as well as keep the if distance > 75 then thing running so as soon as the distance is greater than 75 it will delete them?

local player = script.Parent.Parent.Parent
table.foreach(workspace:GetChildren(),
    function(i,v)
        if v.ClassName == 'Model' and v ~= player and v.Torso ~= nil then
            local distance = (player.Character.Torso.Position-v.Torso.Position).magnitude
            if distance <= 75 then
                local billgui = Instance.new('BillboardGui',script.Parent)
                local infogui = Instance.new('TextLabel',billgui)
                billgui.Active, AlwaysOnTop, Enabled = true, true, true
                infogui.Active = true
                billgui.Size = UDim2.new(0,100,0,100)
                infogui.BackgroundTransparency, BorderSizePixel = 1, 1
                infogui.Position = UDim2.new(0,-50,0,0)
                infogui.Size = UDim2.new(0,200,0,50)
                infogui.FontSize = 'Size24'
                infogui.Text = (v.Name)..' Lvl: '..(v.Level.Value)
                billgui.Adornee = v.Head
                if distance > 75 then
                    billgui:remove()
                    infogui:remove()
                end
            end
        end
end)

1 answer

Log in to vote
2
Answered by 10 years ago

Try using the ChildAdded method. It fires when a child is added to a specific place. Try this code below (It is untested):

local player = script.Parent.Parent.Parent
game.Workspace.ChildAdded:connect(function (child)
    -- Add your code here that you need, child is the object added to the workspace.
end)

This code can be modified, but should work fine for your purposes! I hope this helps, unless I read the entire question wrong.

0
not fully, it does help a little but because it brought a new idea to mind User#600 0 — 10y
Ad

Answer this question