hi im new to roblox studio and coding/scripting in general , im making a mining game where you progress through your own cave and random ores can spawn for you to mine and sell with different rarities and types etc , im trying to write my own script for spawning the ores but i cant seem to get it to work , can anyone help me figure out whats wrong with my script?
#Random spawning script isnt working
1 messages · Page 1 of 1 (latest)
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local cavesFolder = game.Workspace.Caves
local nodesFolder = replicatedStorage.Nodes
local iron = nodesFolder.ironNode
local ruby = nodesFolder.rubyNode
local emerald = nodesFolder.emeraldNode
local diamond = nodesFolder.diamondNode
local nodeTypes = {iron, ruby, emerald, diamond}
players.PlayerAdded:Connect(function(player)
for _, cave in cavesFolder:GetChildren() do
local occupied = cave:FindFirstChild("Occupied")
local owner = cave:FindFirstChild("Owner")
if occupied and occupied.Value == true and owner and owner.Value == player.Name then
local tunnel = cave:FindFirstChild("Tunnel")
local nodeSpawn = tunnel and tunnel:FindFirstChild("nodeSpawn")
if tunnel and nodeSpawn then
local randomNode = nodeTypes[math.random(1, #nodeTypes)]:Clone()
randomNode.Parent = game.Workspace
randomNode.Position = nodeSpawn.Position
end
end
end
end)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cavesFolder = workspace:WaitForChild("Caves")
local nodesFolder = ReplicatedStorage:WaitForChild("Nodes")
local iron = nodesFolder:WaitForChild("ironNode")
local ruby = nodesFolder:WaitForChild("rubyNode")
local emerald = nodesFolder:WaitForChild("emeraldNode")
local diamond = nodesFolder:WaitForChild("diamondNode")
local nodeTypes = {iron, ruby, emerald, diamond}
Players.PlayerAdded:Connect(function(player)
print("Player joined: " .. player.Name)
for _, cave in pairs(cavesFolder:GetChildren()) do
print("Checking cave: " .. cave.Name)
local occupied = cave:FindFirstChild("Occupied")
local owner = cave:FindFirstChild("Owner")
if occupied then
print("Occupied: " .. tostring(occupied.Value))
else
print("No 'Occupied' found in " .. cave.Name)
end
if owner then
print("Owner: " .. owner.Value)
else
print("No 'Owner' found in " .. cave.Name)
end
if occupied and occupied.Value == true and owner and owner.Value == player.Name then
local tunnel = cave:FindFirstChild("Tunnel")
if tunnel then
print("Tunnel found in " .. cave.Name)
local nodeSpawn = tunnel:FindFirstChild("nodeSpawn")
if nodeSpawn then
print("nodeSpawn found, spawning node...")
local randomNodeTemplate = nodeTypes[math.random(#nodeTypes)]
local randomNode = randomNodeTemplate:Clone()
randomNode.Parent = workspace
-- Use CFrame to copy position and orientation
** You are now Level 1! **
hmm still didnt work
i just checked over all my objects in the explorer , all the names are the same
just to be sure, you did make this script with localscript right
needs to be a localscript.
if its a localscript , wouldnt only the owner of the cave be able to see the ores? i want others to be able to see them as well but not mine them if it isnt theirs
oh shit i misread you
You put the ore spawning code inside a regular Script under ServerScriptService, which is correct for spawning ores, but the event you’re listening to only runs for players who join after the script starts
If you start the game with players already in it (like when testing in Roblox Studio), the PlayerAdded event won’t run for those players, so no ores spawn for them
replace the : Players.PlayerAdded:Connect(function(player)
-- spawn ores for player
end)
with
``local Players = game:GetService("Players")
-- ... (other variables setup) ...
local function spawnOreForPlayer(player)
-- your spawning logic here
end
-- THIS: run for players already in game
for _, player in pairs(Players:GetPlayers()) do
spawnOreForPlayer(player)
end
-- THIS: run for players joining after script starts
Players.PlayerAdded:Connect(spawnOreForPlayer)
``
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
** You are now Level 2! **
Make sure you have a folder named Caves in Workspace.
local nodeSpawn = tunnel:FindFirstChild("nodeSpawn")
i noticed this line was looking for nodeSpawn to be a child of tunnel
which it wasnt at first
but i made all the nodeSpawns children of the tunnels
** You are now Level 5! **
yeah thats exactly it
and i added a task.wait
just make sure it works
You need to set the PrimaryPart of the model’s CFrame, or move the whole model by setting its PrimaryPart.CFrame.
yes all of the nodes are models
randomNode:PivotTo(nodeSpawn.CFrame)
kinda high fetched but you should try adding geodes with random chances to get rare shit
keeps player engagement, solely for farming
glad your script works tho!
i have a lot of ideas for this game , just trying to figure out how to get everything working as intended 😄 thanks for the help
if i wanted to specify things like how many nodes can spawn at once , the time in between ores spawning , etc how do you guys think i can go about doing those kinds of things? would i add on to the script i just made or would those have to be separate scripts?
use a while loop
use an attribute or int value somewhere to keep track of the amount of currently spawned nodes
check if the value of the attribute or intvalue is above the max amount, if it is then continue (skip over the current iteration / ignore the rest of the code after that conditional)
otherwise spawn in the node and move it to the desired position
add a delay so studio doesent crash