#item spawning code inefficiency

124 messages · Page 1 of 1 (latest)

devout cipher
#

INTENTION: i have non-collided parts with a script to spawn a random item inside of them when a previously spawned item is moved out of range or gets destroyed
PROBLEM: i will have a very large amount of these item spawnpoints and i want to change the script from using a while loop to a function that detects when the item is moved at all (something like handle.GetPropertyChangedSignal("Position")), but when i put this function in the code, it was getting the object a bit too early (getting a nil value) so the function wouldnt work
QUESTION: how should i replace these lines of code and so it or the game doesnt shit itself

#

i just want to make it fully functional and go to bed

#

i weep

molten spear
#

Maybe put task.wait(itemRespawnTime) after spawnObject() instead? I'm not entirely sure on what I see here and what functions what

devout cipher
#

itemSpawn is a basepart with only child being the script

#

another script adds an item as a child to the basepart

molten spear
#

There's no wait for ItemRespawnTime, I think.

#

It's just an attribute

devout cipher
#

a modulescript handles it

#

sets the attribute

molten spear
#

Ah. What is the modulescript then?

devout cipher
#

just sets it to 15

molten spear
#

Can you also show the code? Kinda hard to help if you don't show me the code

devout cipher
#

there is no issue in the modulescript and its related scripts

#

the issue is in this one

molten spear
#

Okay but there could be a problem with the modulescript that it doesn't tell you

devout cipher
#

its just a number value

#

i already tested it

#

it works

#

i want to make the code look something more like this but it retrieves handle too early and freaks out

molten spear
#

I honestly have no idea then.

#

I'm stumped

plush onyx
#

@devout cipher

devout cipher
#

Hello

plush onyx
#

may I know where are you defining the part variable

#

hi

#

as far as I see you're creating the part variable as a parameter for the position of the childadded function

#

however this will only work inside that function, thats the origin of the problem

#

I may be wrong tho

devout cipher
#

you are likely right

plush onyx
#

alright so

#

as I see

#

you're trying to achieve than when an item spawns on the itemspawn it re-assigns variable item to part and re-assigns the variable handle to the handle of the new part created

devout cipher
#

yes

plush onyx
#

what is part supposed to be

#

like

#

is the part the same parent of handle

#

if thats the case you can replace part with handle.parent and that will fix it

devout cipher
#

yes

#

well

plush onyx
#

replace the underlined part text with handle.Parent.Parent

#

handle.Parent.Parent = game.ReplicatedStorage

devout cipher
#

when i click play, a random model from randomspawn gets added as a child to itemspawn

#

fyi

plush onyx
#

thats coming from another script, you should find it

#

where is the script located?

#

in the explorer

devout cipher
#

serverscriptservice

#

it does it once and then the spawner scripts are supposed to do all the rest

plush onyx
#

which script is located at serverscriptservice

#

this one

#

of the one we were talking about before

devout cipher
#

the one that we were talking about before is a child to the spawn location part

#

this one is in serverscriptservice

plush onyx
#

now let me see the other one

#

rq

plush onyx
devout cipher
#

its quite simple

plush onyx
#

and which is the problem with this one?

devout cipher
#

i may have misunderstood something

#

this one has no issues

plush onyx
#

try it out and tell me how it goes

devout cipher
#

oops i accidentally left that in

plush onyx
#

dw

devout cipher
plush onyx
devout cipher
#

second script as in the server script service one ?

#

if yes then yes

plush onyx
#

is baba printing?

devout cipher
#

it does print but it prints after the error

plush onyx
#

so it is executing before the handle gets actually redirected, try adding a task.wait() statement at the start of the script after creating the values

devout cipher
#

it refused

plush onyx
#

wdym

devout cipher
#

should i be getting the part in a completely different way

plush onyx
#

ye

devout cipher
#

i do not have that much experience with code

#

help me

plush onyx
#

imma ask from zero

#

why is the handle position changing

devout cipher
#

i am quite stumped

devout cipher
#

the two prints at the beginning ?

#

i think i accidentally left a script on

#

hold

plush onyx
#

like

#

what is the objective of the handle.GetPropertyCHangedSignal("Position") function

devout cipher
#

okay so the base intention is to parent the item to some other place when it gets too far away from the spawn location, thus triggering the spawn of another item

#

the magnitude checks do that

plush onyx
#

and the handle is a tool right?

devout cipher
#

the handle is parented and is the primarypart of a model

#

theres a prompt that lets you pick it up, destroying the model and giving you a tool

#

but that is less important now

plush onyx
devout cipher
#

functionak

#

then ingame

plush onyx
#

alr I see

plush onyx
#

the first one

devout cipher
#

its parented to the spawn locations

plush onyx
#

so, I assume the problem is that the handle function is executed for the first time when the script is first started

#

at this moment the handle is nil because you set it to nil

#

you could make a script inside the handle to get the handle this way and not on a global script

#

so you secure it never is nil

#

my advice is to make the position comparison inside a script on every handle and not on a global script like you're doing rn

devout cipher
#

for that script

#

should i:

#

check if parent is nil

plush onyx
#

no because the script will be parented to the handle

devout cipher
#

then check if script.parent.parent.parent is a spawn location

plush onyx
#

if the parent is nil the script does not exist

devout cipher
#

fuc

#

yea

plush onyx
#

place a script inside the handle and everytime the script changes its parent check if the parent is a spawn location and do the function if that happens

devout cipher
#

good

#

im saving this

#

i will fix it tomorrow