#Assign a cave when player joins

1 messages · Page 1 of 1 (latest)

sharp bison
#

create a folder where you have all of the caves stored, then use a for loop on folder:GetChildren() and check whether or not the cave is occupied, if it isn't then pivot the character to the cave and mark it as occupied

#

to display their name over the top of the cave you could either use a surface gui or billboard gui but surface gui would be better i think

solid spade
bleak lichenBOT
#

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

sharp bison
#

yes it would be in a script in serverscriptservice

#

you would do the player added event using the players service

#

and then iterate through the folder

solid spade
#

sorry i might have to do a little more research before i try this

#

i dont think i know enough to do this yet

#

thank you anyway though 🥹

sharp bison
#

youre welcome

#

you should probably watch brawldev

#

he has good tutorials

solid spade
#

thanks ill check him out!

sharp bison
#

do you want me to give you the code for this now or do you want to figure out how to do it yourself, i personally think the second option is better though

solid spade
#

i think seeing how the code actually looks laid out would help me understand it more , so if you can that would be great!

#

im not gonna copy and paste it in my game ill just use it for reference , id like to make all my scripts myself to get a better understanding of it all

sharp bison
#

wait let me format it

#
--[[
Before doing this, in the workspace.Caves folder, make sure that every cave model has a 
boolvalue underneath it named Occupied set to false
Also make sure that you create a spawn location for each cave
]]

local Players = game:GetService("Players") -- we are getting the players service
local cavesFolder = workspace.Caves -- we are referencing the caves folder in workspace

Players.PlayerAdded:Connect(function(player) -- this event "PlayerAdded" fires every single time a player joins the game and passes the player
    for _, cave : Model in cavesFolder:GetChildren() do -- get children returns all of the children of the caves folder, like cave1, cave2, etc.
        local occupied : BoolValue = cave:FindFirstChild("Occupied") -- we then get the occupied boolvalue that is a child of the cave
        local spawnLocation = cave:FindFirstChildOfClass("SpawnLocation") -- as well as the spawn location
        
        if not occupied.Value then -- this checks whether or not the cave is occupied, it essentially means, if occupied.Value == false, which means is this cave occupied?
            occupied.Value = true -- if it is not occupied, then we make it occupied, by setting the value to true
            player.RespawnLocation = spawnLocation -- we set the player's respawn location to the spawn location, so that every time they die, they get teleported to that spawn location
            local character = player.Character or player.CharacterAdded:Wait() -- this gets the character model of the player, or prevents the following line of code from running until the character has fully loaded into the game
            character:PivotTo(spawnLocation.CFrame + Vector3.new(0, spawnLocation.Size.Y, 0)) -- we move the character model to the position of the spawnLocation + the size of it on the y axis
        end
    end
end)
sharp bison
#

9 minutes

bleak lichenBOT
#

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

sharp bison
#

i think

remote bough
#

alr

sharp bison
solid spade
sharp bison
#

if you get an error you might have skipped a step in the instructions in the beginning

solid spade
#

so i should have seperate "SpawnLocation" under all of my cave models? the ones that say Cave1 Cave2 etc?

sharp bison
#

yea so that the owner of that cave can respawn in that same place

#

unless you dont want to do that then you can just remove those lines

solid spade
bleak lichenBOT
#

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

solid spade
#

everything else is making sense to me now though

#

its this specific part that i dont really understand

#

_, cave : Model

#

whats the underscore doing here?

solid spade
#

okay i actually understand the code you made now , you can disregard that last question i asked

sharp bison
#

_ is a place holder or unused variable

solid spade
#

i ended up using your code and just made different comments more suitable for me to remember what everything means , i couldnt figure out how to make this script myself without just doing what you did , assuming there are other ways to do this

sharp bison
#

but if you did end up using it it would give you the current index in the loop

#

do you know for loops?

solid spade
#

yeah i learned about them a bit when learning python

sharp bison
#

so instead of using for i in range you do for i =

#

like this:

python:
for i in range(1, 6):
print(i)

lua:
for i = 1, 5 do
print(i)
end

solid spade
#

ohhhh i get it

#

thank you for all the help!

#

im gonna close this for now 😄 thanks again for helping me understand scripting a bit more