#Random spawning script isnt working

1 messages · Page 1 of 1 (latest)

deft kettle
#

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?

#

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)

ivory current
#

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
calm adderBOT
#

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

ivory current
#

randomNode.CFrame = nodeSpawn.CFrame
else
print("No 'nodeSpawn' found in Tunnel of " .. cave.Name)
end
else
print("No 'Tunnel' found in " .. cave.Name)
end
end
end
end)

#
  • put them tgther
deft kettle
#

hmm still didnt work

ivory current
#

are the names correct?

#

scripts are picky, cap sensitive

deft kettle
#

i just checked over all my objects in the explorer , all the names are the same

ivory current
#

just to be sure, you did make this script with localscript right

deft kettle
#

its a regular script under serverscriptservice

ivory current
#

needs to be a localscript.

deft kettle
#

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

ivory current
#

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)
``

deft kettle
#

aww man now im a lil confused

#

i changed 2 things though

ivory current
calm adderBOT
#

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

ivory current
#

Make sure you have a folder named Caves in Workspace.

deft kettle
#

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

calm adderBOT
#

studio** You are now Level 5! **studio

ivory current
#

yeah thats exactly it

deft kettle
#

and i added a task.wait

ivory current
#

just make sure it works

deft kettle
#

i see an ore now but its in the middle of the main area instead of inside my cave

ivory current
#

You need to set the PrimaryPart of the model’s CFrame, or move the whole model by setting its PrimaryPart.CFrame.

sick wyvern
#

is rubyNode a model

#

try using pivot to instead

deft kettle
#

yes all of the nodes are models

sick wyvern
#

randomNode:PivotTo(nodeSpawn.CFrame)

deft kettle
#

omg you guys are geniuses

ivory current
#

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!

deft kettle
#

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?

sick wyvern
#

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