#simple generation script

1 messages · Page 1 of 1 (latest)

fluid tide
#

i need help, i want to make a script so that when i touch the checkpoint brick, it will generate randomly one of these from replicatedstorage. (image 1)
so, when i touch the checkpoint, i want it to generate a random one of these 75 studs ahead of it.

#

im not horrid at scripting, but i cannot figure this out for the life of me

eternal lantern
#

-- Variables
local checkpoint = script.Parent -- The checkpoint brick
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Put your models/parts in a folder inside ReplicatedStorage
local itemsFolder = ReplicatedStorage:WaitForChild("Items") -- change "Items" to your folder name

-- Settings
local spawnDistance = 75 -- studs ahead

-- Function to spawn a random item
local function spawnRandomItem()
-- Pick a random child from the folder
local children = itemsFolder:GetChildren()
if #children == 0 then return end -- no items

local randomItem = children[math.random(1, #children)]

-- Clone and position it
local clone = randomItem:Clone()
local spawnPosition = checkpoint.Position + (checkpoint.CFrame.LookVector * spawnDistance)
clone:SetPrimaryPartCFrame(CFrame.new(spawnPosition))

clone.Parent = workspace

end

-- Event when player touches checkpoint
checkpoint.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
spawnRandomItem()
end
end)

#

Script using ChatGPT should work

#

Place the script in ServerScriptService and change the directories to those which you mentioned in your Q

fluid tide
#

small issue, i want this to generate infinitely so it would have to be in each module

thick fableBOT
#

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

eternal lantern
#

Infinite as in time or infinite as in per model?

fluid tide
#

like if i touch the checkpoint of a module, it will generate another and then when i touch the next, another

#

so on and so forth

eternal lantern
#

Ah ok

jagged latch
#

use collection service

eternal lantern
#

So per model

fluid tide
#

basically a bridge that builds itself as i progress

eternal lantern
#

Change the local checkpoint = script.Parent -- The checkpoint brick into local checkpoint = {}

thick fableBOT
#

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

eternal lantern
#

Then go for _, child in [folder with all the modules you are talking about] do table.insert(checkpoint, child) end

#

Did it work?

fluid tide
#

2 secpmds

#

local checkpoint = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local itemsFolder = ReplicatedStorage:WaitForChild("Items")
local checkpointFolder = workspace:WaitForChild("Checkpoints")

for _, child in pairs(checkpointFolder:GetChildren()) do
table.insert(checkpoint, child)
end

local spawnDistance = 75

local function spawnRandomItem(checkpointPart)
local children = itemsFolder:GetChildren()
if #children == 0 then return end

local randomItem = children[math.random(1, #children)]
local clone = randomItem:Clone()
local spawnPosition = checkpointPart.Position + (checkpointPart.CFrame.LookVector * spawnDistance)
clone:SetPrimaryPartCFrame(CFrame.new(spawnPosition))
clone.Parent = workspace

end

for _, checkpointPart in pairs(checkpoint) do
checkpointPart.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
spawnRandomItem(checkpointPart)
end
end)
end

like this?

eternal lantern
#

good but have _ after the fore

fluid tide
#

local part = script.Parent
local module = part.Parent
local checkpointsFolder = workspace:WaitForChild("Checkpoints")
local Players = game:GetService("Players")

part.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
local modules = checkpointsFolder:GetChildren()
if #modules > 0 then
local chosenModule = modules[math.random(1, #modules)]:Clone()
local offset = module.PrimaryPart and module.PrimaryPart.CFrame.LookVector or Vector3.new(0, 0, 1)
local newPos = module:GetPrimaryPartCFrame() * CFrame.new(0, 0, -75)
chosenModule.Parent = workspace
chosenModule:SetPrimaryPartCFrame(newPos)
end
end
end)

found a very simple fix, thanks always