Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make my random house spawn script work?

Asked by 2 years ago

So I'm trying to make a game like horrific housing, where houses will spawn for every player in the server, so 3 players 3 houses will spawn, and I have wrote some code and a table incase I need to use a table for this, but the code won't work, it's not giving me any errors in the output.

local Houses = {
    [1] = game.ServerStorage.House1,
    [2] = game.ServerStorage.House2,
    [3] = game.ServerStorage.House3,
    [4] = game.ServerStorage.House4,
    [5] = game.ServerStorage.House5,
    [6] = game.ServerStorage.House6,
    [7] = game.ServerStorage.House7,
    [8] = game.ServerStorage.House8
}

while true do
    wait(1)
        if game.Players:GetPlayers() == 1 then
            if game.Workspace.House1 then
        return
    else
            game.ServerStorage.House1:Clone(game.Workspace)
        end
    end
end
0
im surprised this isn't giving you an error. You don't need the extra param on :Clone() and line 15 would error because there is no end. Make sure to put that cloned part into a variable and parent it to workspace using .Parent, make sure to use SetPrimaryPartCFrame to move the house greatneil80 2647 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

I'm not really sure about this, but I don't see anywhere you have written where to place the houses, or that it should spawn a random house.

0
The houses are already in position, and it isn't set to spawn a specific house, I'm trying to get it to spawn one house first so I know that the code will work, the houses are already in place and it CLONES the house to WORKSPACE pokemon9master29 6 — 2y
0
And where's the script part to randomize the house spawns? RunkerSecurity 6 — 2y
0
It's not supposed to randomize the house spawning, it's supposed to spawn the houses in order depending on the amount of players in the game, so it's not just scattered around, but actually organized pokemon9master29 6 — 2y
0
So 3 players equals 3 houses, no more? RunkerSecurity 6 — 2y
0
yeah, but I havn't added in the spawning for the other houses because I'm trying to get atleast the first one to work pokemon9master29 6 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Try this:

game.Players.PlayerAdded:Connect(function)
local AmountOfPlayers = #Players:GetPlayers()
if workspace:FindFirstChild("House"..AmountOfPlayers) then
print("House is allready spawned") 
else
if game.ServerStorage:FindFirstChild("House"..AmountOfPlayers) then
local house = game.ServerStorage::FindFirstChild("House"..AmountOfPlayers):Clone()
house.Parent = workspace
else
warn("Not enough houses for ammount of players in server")
end
end

How this works? Whenever a player joins, it will check how many players there is currently. It will then check if that number of house is spawned. by doing workspace:FindFirstChild("House"..AmountOfPlayers) e.g if its 1 person. It wil be workspace:FindFirstChild("House1") If its not spawned, it will do the same function to find it in server storage and clone it into the map If its not in server storage, it will output a warning message into console telling you there is not enough houses.

This is a helpful link for detecting how many players is in a server

Answer this question