#help me with some basic scripting thing

1 messages · Page 1 of 1 (latest)

steel glacier
#

Hello, I'm new to luau and scripting/game development in general. I inserted an script inside a human model, with the idea of turning off the colliders by using CanCollide = false. The thing is, because of this is considered an humanoid, the properties of all the MeshParts will be reseted when entering the game, so turning all of them to false by hand won't work. This also means that the script will load first than the MeshParts in the model.

The script I made works, but obviously, making it so the task will wait 1.5s before executing is not a good practice at all. I need to do so the script only executes when its sure that all the MeshParts inside the model has been fully loaded, not before, not after. May I get some help? yvl


task.wait(1.5)

for i, part in ipairs(partsAhh) do
    if part:IsA("BasePart") then
        part.CanCollide = false
    end
end
rocky solar
#

If u delete the task.wait(1.5) the script gives any error?

steel glacier
#

It simply doesn't work and the npc has its colliders = true

shell orbit
#

theres .ChildAddedevent

#

and ..DescendantAdded

#

Just connect to descendantadded event

#

change there

#

and when script first runs get all descendants and run on them (cuz some might load before script)

steel glacier
#

I'll try that

echo cove
#

waitforchild

steel glacier
sleek sailBOT
#

studio** You are now Level 1! **studio

steel glacier
#

waitforchild will only wait for the container (the model) but won't for the descendants of it

unborn warren
steel glacier
#

Ahh, you mean for the model?

#

So I treat him as a player or?

unborn warren
#

My apologizes

#

I thought youre talking bout player model

#

Im pretty sure that WaitForChild should work

sleek sailBOT
#

studio** You are now Level 1! **studio

steel glacier
#

._.

#

I alr said that

#

cause I've tried

#

is there even a way I can use WaitForChild referring to all the meshparts of the model?

unborn warren
#

No

#

U must loop throught them in order to check if all parts are loaded

#

Or

#

Just check if PrimaryPart is loaded

#

The simplest way might be checking if primary part of a model is loaded because it might mean that all of the part also are

#

If you want to be sure make custom function that loops throught all instances in your model

steel glacier
#

I've tried everything

steel glacier
#

it doesn't work

shell orbit
steel glacier
#

w8

#

It doesn't even load the childs of the model

#

They don't count

#

It seems they load faster than the script

#

lemme take cap rq

#

I deleted it

#

where do I put the Script

#

it'll be a normal script that will print "ChildAddedEvent Works" just to check if it works

#

whatever

sleek sailBOT
#

studio** You are now Level 2! **studio

steel glacier
#

This is with the script on the model

shell orbit
steel glacier
#

w8

steel glacier
#

I'm gonna try what you said again, I just tried this, it doesn't work

shell orbit
steel glacier
shell orbit
#

no

#

any

#

added

steel glacier
#

yeah, I mean

#

any of them, yes, but I need ALL of them to be fully loaded

#

to then use this

    if childs:IsA("BasePart") then
        childs.CanCollide = false
    end
end```
shell orbit
#
local woman = workspace.woman

woman.ChildAdded:Connect(function(child)
if child:IsA("BasePart") then child.CanCollide = false end
end)

for i,v in woman:GetChildren() do
if v:IsA("BasePart") then v.CanCollide = false end
end
#

It might be childAdded

#

i forgot the capitalization

steel glacier
#

you can't call for child

#

in the second function

#

cause the first one ends

#

Ok so, I'm not an expert

#

can you send me a code that should work so I can learn from it?

#

I've tried everything

shell orbit
steel glacier
#

what is child?

#

I'm asking

shell orbit
#

ye fixed whatever

steel glacier
#

No, it doesn't work

#

But I found the solution

shell orbit
#

?

steel glacier
shell orbit
steel glacier
#

ok so this is the one u tried

#

am I right?

shell orbit
#

ig?

steel glacier
#

well it doesn't work. Apparently Roblox objects load in different ways depending on if its the first or the second time you've entered the server. It basically reutilices the previous version of the model and makes a new one in every new "server" created. So all this time we've been "calling" for the previous version or something like that i don't fucking know I'm still trying to understand.

I asked chatgpt 1 million times just for curiosity and it gave me a code that worked the first time, and the second didn't, but the third did, and the fourth did... etc...

Until I polished some stuff and I got this code, that must be a localScript and must be on StarterCharacter/PlayerScripts.

#

by using task.spawn that I've never used

#

btw, print shows only 7 children

#

The model has like 21

steel glacier
#

nvm it works without using task.spawn

sleek sailBOT
#

studio** You are now Level 3! **studio

unborn warren
steel glacier
#

If you can give me a better solution, I'll do it

unborn warren
#

I told you

steel glacier
#

and how do I check it

#

I told u, I got 3 days into scripting

steel glacier
#

how bro felt after that

#

sir I'm waiting for your response

unborn warren
#

Sorry

#

I was doing something

steel glacier
#

ok

unborn warren
#

Go in model properties and there is a PrimaryPart you should probably set

steel glacier
#

nobody told me abt PrimaryPart

unborn warren
#

Its a main part of a model, and origin of a model

#

But whatever

steel glacier
#

It is the HumanoidRootPart

#

this?

unborn warren
#

Yea

#

Leave it be

#

Check if this part is just loaded

steel glacier
#

so if the HumanoidRootPart is loaded, it means that the model has been fully loaded?

unborn warren
#

Probably

#

That's the easieast way to make it

steel glacier
#

I do that inside a script inside the Model right?

#

smt like this?

unborn warren
sleek sailBOT
#

studio** You are now Level 2! **studio

unborn warren
#

But i'm curious

#

Why do u want to set all of the parts so they wont collide?

#

Is your npc is anchored?

steel glacier
#

Something like that?

#

Because that doesn't work either

unborn warren
steel glacier
#

fr I want to find some other way to do it

unborn warren
#

Gimme a second then

#

@steel glacier But i still dont know your purpose

#

What are u trying to attempt

steel glacier
#

nothing

unborn warren
#

Your npc has name ghost so prolly smth related with zero collisions

steel glacier
#

literally nothing, I just want to be able to walk through

#

yes

#

basically that

#

the challenge was to make his canCollide properties = false

#

while having an humanoid as a child

unborn warren
sleek sailBOT
#

studio** You are now Level 3! **studio

unborn warren
#

The only part in our characters that has collisions on is a torso

#

Just look for a torso and disable its collisions

steel glacier
#

yes ik

unborn warren
#

simple as that

steel glacier
#

u can't

unborn warren
steel glacier
#

because of having humanoid as a child, the game restart its properties

#

try it yourself

unborn warren
#

U got me here

steel glacier
#

yes man 😂 that was the start of the problem

unborn warren
#

Give me 5 minutes

unborn warren
#

@steel glacier

#

That's the most optimal way i've found

local character: Model = script.Parent
local Humanoid: Humanoid = character:WaitForChild("Humanoid")


task.delay(2, function()
    for _, part in character:GetChildren() do
        if part:IsA("BasePart") then
            part.CanCollide = false
        end
    end
end)

Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
#

If u set humanoids state to Physics it won't change any properties for character if he changes a state

#

But its at cost so he cant move tho

#

But u cant skip this changestate part

#

It shouldn't interrupt tbh

steel glacier
#

I need him to be movible

steel glacier
unborn warren
unborn warren
#

Get all npc's in a table and loop throught them in task.delay function if u want them to be non colliable on run time

#

Use CollectionService tbh

#

Its easier to get all things u want in one place

#

Tag your npc's with tag dunno "npc" and look for all of the objects with this tag

steel glacier
#

I told you, I've tried making a task.wait(2), but in a normal game, I don't know how many items are going to load, so it's not sure that the model will be loaded in 2 seconds

sleek sailBOT
#

studio** You are now Level 4! **studio

echo cove
#

waitforchild():getchildren

#

ez

unborn warren
#

but that's too much of a work and not worth it

steel glacier
steel glacier
#

and It worked

echo cove
#

It was not in local script???

#

That explains a lot

unborn warren