#improving random item spawning

1 messages · Page 1 of 1 (latest)

ancient lake
#

so as of now, the script i have made gets a table of spawnpoint parts, gets a table of items it could spawn and then spawns them at the cframes of every existing spawnpoint, so far so good
what im wondering is, how would i go about spawning a random item when an item is taken away from a spawnpoint or destroyed (restocking the spawnpoint)?
i do not wish to have anyone code the entire thing for me, i just want to be guided in the right direction or be given code snippets or links that could be helpful
would i have to do something with touch events?

versed wagon
#

have your table of spawn locations track what item is at the location or each item track what spawn location it is at then you now know which spawn location had its item picked up so you can then know to respawn a new item there

#

ez

ancient lake
#

uuuuuhh

ancient lake
versed wagon
#

table

ancient lake
#

not exactly "more detail" but okay ill see what i can do

versed wagon
#

tables very versatile. practice them

ancient lake
#

could i do something like parenting the spawned items to the spawnpoints themselves and then having a script in the spawnpoints that waits for an ancestry changed event, then when it detects that theres no item anymore it gets the random item table and spawns a random item again after like 10 minutes

versed wagon
#

probably

ancient lake
#

would also need to add a failsafe if the player somehow managed to throw the item into irretrievable oblivion without destroying it

#

not quite sure how to approach this

versed wagon
#

death plane destroys parts at -500

#

but if its in the world and out of reach then yea haha it gone

#

idk, you sound like you got this

ancient lake
#

maybe

versed wagon
#

have at it and just try different stuff

ancient lake
#

what can i do with position.magnitude

versed wagon
#

lol

#

yea keep practicing. no one fully understands what they're doing when trying something for a first time

ancient lake
#

i track the child of the spawnlocation

#

when its position changes i check for position magnitude relative to spawnlocation

#

if its too high

#

i put child into foster parent

#

spawnlocation cry and make another child after enough time

#

would this be inefficient or ugly

versed wagon
ancient lake
#

im asking theoretically

versed wagon
#

priorities bruh. get it working first, and then maybe consider if your solution is trash, as opposed to considering if your proposed solution which doesn't exist yet is efficient and flawless and then putting pen to paper and trying to get it working.

#

it's probably the biggest mistake a beginner can make.

ancient lake
#

im having the fun

versed wagon
#

as a beginner you're going to write trash code, you can't avoid it. embrace the jank spaghetti code, keep calm, and make games 👍

versed wagon
ancient lake
#

my foolish ass accidentally forgot to set the proper position of a raycast that detects the highest point my ghoulish abominations could spawn at so they started spawning in my basement and mauling me

#

but i fixed it already so

#

how amusing

versed wagon
ancient lake
#

hmm

versed wagon
#

like the fact that rbxscriptconnection's don't inherit from the Object class (which should give it an :IsA() function)

#

that's just stupid on roblox's part, the docs lie 😮‍💨

ancient lake
#

there are many systems in my game that track if its day or night

#

do i make it into a module script somehow

versed wagon
#

docs say every class should inherit from object, yet clearly that's not true, example right there. in what world of perfect efficiency would you have known the docs were wrong? you see, you cannot account for everything.

#

idk modulescript, lighting.timeofday just play with stuff.

#

there's always more than 1 way to skin a cat.

ancient lake
#

i should add cat eating into my game

versed wagon
#

and in programming, infinite ways to do pretty much anything.

ancient lake
#

players can already eat my not human humanoid monsters

#

okay ill see what i can do with what i just conspired

#

oh what the fuck

#

oops

violet elk
#

o i know you

#

did you figure out the windmill

ancient lake
#

yers

#

very clean very awesome

violet elk
#

nice

ancient lake
#

i have figured out many things since then

#

but back to the matter

ancient lake
#

@versed wagon sir i am blind why did it spawn ten thousand of my items

#

did my descendant removing function run that many times

#

save me print

#

its because of the children of the item

#

ill just make it detect one of the parts inside

#

apologies for the ping

#

i did it

fair pond
ancient lake
#

conspiring with childadded and childremoved

fair pond
#

My phone is lagging and I just typed that out just for everything to load once it sent

ancient lake
#

mmm

ancient lake
#

i really dont wanna use any while loops to check the position of items

#

how do i make the getpropertychangedsignal function wait until the handle is defined
how should i do this differently

#

@versed wagon return for one last time smart fella i am foolish

quick baneBOT
#

studio** You are now Level 7! **studio

warm thorn
ancient lake
#

how much will the funeral cost

#

😢

fair pond
#

Somewhere above and then change it in the script

ancient lake
#

i did try that already

#

hold

fair pond
#

Do you know what I mean?

ancient lake
#

yer

fair pond
#

So why didn’t it work

#

Can you send a pic of what u did

ancient lake
#

ill have to try again

#

since i discombobulated the code

#

hold

#

@fair pond

fair pond
#

So one big function instead of 2

ancient lake
#

but ill try it again

fair pond
#

And error

ancient lake
#

-- Services --
local ServerStorage = game:GetService("ServerStorage")

-- Script --
local itemsFolder = ServerStorage.Items.RandomSpawn

local itemSpawn = script.Parent
local items = itemsFolder:GetChildren()
local item = nil
local handle = nil

local itemRespawnTime = itemSpawn:GetAttribute("ItemRespawnTime")
local debounce = false

local function spawnObject()
local clone = items[math.random(1, #items)]:Clone()
clone:SetPrimaryPartCFrame(itemSpawn.CFrame * CFrame.new(0, 0.5, 0) * CFrame.Angles(0, 0, 90))
clone.Parent = itemSpawn
end

itemSpawn.ChildAdded:Connect(function(part)
print("baba")
item = part
handle = part:FindFirstChild("Handle")

handle:GetPropertyChangedSignal("Position"):Connect(function()
    print((itemSpawn.Position - handle.Position).Magnitude)
    if (itemSpawn.Position - handle.Position).Magnitude > 10 then
        handle.Parent.Parent = workspace.ReplicatedItems
    end
end)

end)

itemSpawn.ChildRemoved:Connect(function()
task.wait(itemRespawnTime)
spawnObject()
end)

#

there are no errors

#

i think that function might only be active for that one singular moment that the childadded function is active

fair pond
#

Try adding a wait() or a wait(0.2) after you define the handle, it might not be fully loaded