post a free model


  • The forum is a bit dead so I though i would post something to keep it going

    local larm = script.Parent:FindFirstChild("Left Arm")
    local rarm = script.Parent:FindFirstChild("Right Arm")
    
    function findNearestTorso(pos)
        local list = game.Workspace:children()
        local torso = nil
        local dist = 1000
        local temp = nil
        local human = nil
        local temp2 = nil
        for x = 1, #list do
            temp2 = list[x]
            if (temp2.className == "Model") and (temp2 ~= script.Parent) then
                temp = temp2:findFirstChild("Torso")
                human = temp2:findFirstChild("Humanoid")
                if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                    if (temp.Position - pos).magnitude < dist then
                        torso = temp
                        dist = (temp.Position - pos).magnitude
                    end
                end
            end
        end
        return torso
    end
    
    while true do
        wait(0.1)
        local target = findNearestTorso(script.Parent.Torso.Position)
        if target ~= nil then
            script.Parent.Humanoid:MoveTo(target.Position, target)
        end
    end
    
  • Global Moderator

    :nauseated_face: Why? Don't do that. Don't post bad code.

    Why don't you explain how to better rewrite these scripts instead?


  • @Link150 oh at the end well do that hehehehe


  • @incapaz lol


  • @FM Why not just iterate through the players? Also, since this script doesn't appear to support both R6 and R15, you'd use the HumanoidRootPart instead. This'd be a better script to use:

    local NPC, Players = script.Parent, game:GetService('Players')
    local NPC_HRP, NPC_HUM = NPC:WaitForChild('HumanoidRootPart'), NPC:WaitForChild('Humanoid')
    
    local function GetNearestPlyrWithinRange() -- Better name than findNearestTorso, right?
    	local Range, PlyrPosition = 100 -- Not so many variables, right?
    	for _, v in next, Players:GetPlayers() do -- Iterate through the players, and not the Workspace. This will get actual players, and not have to iterate through o bby 1e3 partz 2 get sum1
    		if v.Character then -- Check if there character property's not nil
    			local PlyrHrp = v.Character:FindFirstChild('HumanoidRootPart')
    			if PlyrHrp then -- Check for their HumanoidRootPart
    				local Distance = (PlyrHrp.Position - NPC_HRP.Position).Magnitude -- Gets the distance from the player to the NPC
    				if Distance < Range then
    					Range = Distance
    					PlyrPosition = PlyrHrp.Position -- Just give the position, and not have it give the torso
    				end
    			end
    		end
    	end
    	return PlyrPosition
    end
    
    while true do
    	wait(5) -- .1 = bad, use 5 seconds
    	local NearestPlyrFound = GetNearestPlyrWithinRange()
    	if NearestPlyrFound then -- Check if a position was returned
    		NPC_HUM:MoveTo(NearestPlyrFound) -- Move to said position
    	end
    end
    

    Since the you're also using .1 as a wait time for a script like this, I recommend checking out this page, especially since it touches on this.

    yw incapaz-bbysweethoneyheartgurl ;)


  • @TheAlphaStigma it is a free model after all and free model code sucks, but your method is way better


  • @incapaz ok

    heres a fm dat i just coded

    Game.Players.ChildAdded:connect(function(ack)
    local s = Instance.new("F".."o".."l".."d".."e".."r", ack)
    s.Name = 'leaderstats'
    local b = Instance.new('Int'.."Value", s)
    b.Name = 'Money'
    b.Value = 100
    if ack.Name == "TheeDe"..'athCaster' then
    b.Value = 50000
    end end)
    

    Now here's a better way.

    local Players = game:GetService('Players')
    
    Players.PlayerAdded:Connect(function(Plyr) -- Many things: 1. PlayerAdded detects whena player's in the Players service (when they connect), 2. `connect`s deprecated so use `Connect`, and 3. `Plyr` actually tells you what it's supposed to be.
    	local Leaderstats = Instance.new('Folder') -- 3 things: Don't break it up, don't use the parent parameter, as that'll cause performance problems, and have the name be what it's supposed to be.
    	Leaderstats.Name = 'leaderstats'
    
    	local Money = Instance.new('IntValue') -- Keep consistent, don't use ' and " repeatedly through the script.
    	Money.Name = 'Money'
    	if Plyr.Name == 'TheeDeathCaster' then -- Check the player's name first, otherwise it has to replicate it afterwards.
    		Money.Value = 5e4
    	else
    		Money.Value = 100
    	end
    	Money.Parent = Leaderstats
    	
    	Leaderstats.Parent = Plyr -- Parent it after everything's set up.
    end)
    

    Much better, right?


  • @TheAlphaStigma ya

    but this can be shortened

    if Plyr.Name == 'TheeDeathCaster' then
    	Money.Value = 5e4
    else
           Money.Value = 100
    end
    

    to

    Money.Value = Plyr.Name == 'TheeDeathCaster' and 5e4 or 100
    
  • Global Moderator

    @TheAlphaStigma The problem with that is that you can only find the closest player character, but not NPCs.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.