My script, to put a house in a random plot, is not working?

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")
local ds2 = datastore:GetDataStore("CashSaveSystem")
local ds3 = datastore:GetDataStore("jb")
local DS = game:GetService("DataStoreService"):GetDataStore("YourDataStore")
local Plots = game.Workspace.Plots
local StarterHouse = game.ServerStorage.Houses.StarterHouse:Clone()
local DFChildren = Plots:GetChildren() -- Returns a table
local random = DFChildren[math.random(1, #DFChildren)]

game.Players.PlayerAdded:connect(function(plr, player)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	local gems = Instance.new("IntValue", folder)
	gems.Name = "House"
	local cash = Instance.new("IntValue", folder)
	cash.Name = "Cash"
	local jb = Instance.new("IntValue", folder)
	jb.Name = "jb"
	
	gems.Value = ds1:GetAsync(plr.UserId) or 0
	ds1:SetAsync(plr.UserId, gems.Value)
	
	cash.Value = ds2:GetAsync(plr.UserId) or 0
	ds2:SetAsync(plr.UserId, cash.Value)
	
	jb.Value = ds3:GetAsync(plr.UserId) or 0
	ds3:SetAsync(plr.UserId, jb.Value)

	cash.Changed:connect(function()
		ds2:SetAsync(plr.UserId, cash.Value)
	end)
	
	gems.Changed:connect(function()
		ds1:SetAsync(plr.UserId, gems.Value)
	end)
	
	jb.Changed:connect(function()
		ds3:SetAsync(plr.UserId, jb.Value)
	end)
	
	if folder.jb.Value == 0 then
		print("Never Joined Before")
		folder.House.Value = 1
		folder.Cash.Value = 10000
		folder.jb.Value = 1
	else
		print("Joined Before")
	end
	
	if folder.House.Value == 1 then
		StarterHouse.Parent = workspace
		StarterHouse.PrimaryPart.Position = random.Position
	end
end)

This is my script, and it is not working, at the bottom, what It is supposed to do is if the House Value Leaderstat is 1, it will Spawn a house in a random plot, but all its doing its spawning the floor in the plot.

StarterHouse:SetPrimaryPartCFrame(CFrame.new(random.Position.X, random.Position.Y, random.Position.Z))

you have to set the primarypartcframe not the position like @nuttela_for1me said

You also have to move the random choice to inside the if statement, because currently it just picks a random one at the start and gives the same on to all of the players that join.

Just so you know that piece of code is destined to fail

2 Likes

Already Works, If its not broken, I won’t try and fix it and mess it up, but just to question, why will it fail?

It spawns the wrong way round, how would I fix that?

It will fail because of limitations of datastore you can only make playerCount * 6 + 60 request per minute meaning that if you change the value too often it will break also not to mention datastore WILL give error if the data is not recieved so i would recommend having a pcall around it

Ah, I saw something like that in the console.

StarterHouse:SetPrimaryPartCFrame(CFrame.new(random.Position.X, random.Position.Y, random.Position.Z) * CFrame.Angles(0,math.pi,0))

I think this should work

Also forgot to mention that SetAsync can only be called every 5 seconds

Now the other one works, but the original plot does not.
Tried Rotating the Part Too.

I added something to the end of that line. i added CFrame.Angles to the end of it

2021-03-03 09-45-20

So how do I solve this? Above there is a gif showing it.

You have to adjust the CFrame.Angles to the rotation that you need. So on the y value you can write 0 for 0 degrees math.pi/2 for 45, math.pi for 90 and math.pi * 1.5 for 180

But wont that mess the other one up?

the CFrame.Angles() Only controlls the rotation so you can rotate it as much as you wont and it wont mess up annything

How would I make the Door always Face the Road

What are the plots? Are they parts? If so, why not just set to the cframe of the plot part and rotate that target part as needed.

Like this:

StarterHouse:SetPrimaryPartCFrame(random.CFrame))
1 Like

They are parts indeed, ill try this