How could I position a part at the face of the baseplate?

Hi guys. I’m making a system that spawns tokens randomly around the map. However, it’s supposed to stay within the baseplate’s size and not spawning out of bounds or in the void where the player cannot reach it. I’m not great at math, so the token spawns out of bounds or into the void. Please help me with this if you can!

local RunService = game:GetService("RunService")
local Players = game.Players or game:GetService("Players")
local Folder = script.Parent
local Biome = script.Biome -- This is the baseplate. I'm using an ObjectValue to get the Instance as you will see later.

while true do
	task.wait(3)
	local Rarity = math.random(1, 50)
	if Rarity ~= 50 then
		print("Token spawned!")
		local Token = Folder.Token:Clone()
		local Worth = Token.Worth
		local SFX = Token.Token.Collect
		Token.Parent = Folder
		Token.Token.Transparency = 0
		Token.Logo.Decal.Transparency = 0
		Token.Logo.Decal2.Transparency = 0
		Token:PivotTo(Token.WorldPivot * CFrame.new(Biome.Value.Size.X / Biome.Value.Position.X, 12, Biome.Value.Size.Z / Biome.Value.Position.Z))
		Token.Token.Touched:Connect(function(hit)
			if Players:GetPlayerFromCharacter(hit.Parent) then
				local Player = Players:GetPlayerFromCharacter(hit.Parent)
				local leaderstats = Player:FindFirstChild("leaderstats")
				if not leaderstats then
					return
				end
				local Tokens = leaderstats:FindFirstChild("Tokens")
				if not Tokens then
					return
				end
				SFX.Parent = Player
				SFX:Play()
				SFX.Ended:Connect(function()
					SFX:Remove()
				end)
				Tokens.Value += Worth.Value
				Token:Destroy()
			end
		end)
		RunService.Heartbeat:Connect(function(DT)
			Token:PivotTo(Token.WorldPivot * CFrame.Angles(0, 0, -0.1))
		end)
	elseif Rarity == 50 then
		print("Rare token spawned!")
		local Token = Folder.Token:Clone()
		local Worth = Token.Worth
		local SFX = Token.Token.Collect
		SFX.SoundId = "rbxassetid://3302699870"
		Worth.Value = 10
		Token.Parent = Folder
		Token.Token.Color = Color3.fromRGB(200, 0, 0)
		Token.Token.Transparency = 0
		Token.Logo.Decal.Transparency = 0
		Token.Logo.Decal2.Transparency = 0
		Token:PivotTo(Token.WorldPivot * CFrame.new(Biome.Value.Size.X / Biome.Value.Position.X, 12, Biome.Value.Size.Z / Biome.Value.Position.Z))
		Token.Token.Touched:Connect(function(hit)
			if Players:GetPlayerFromCharacter(hit.Parent) then
				local Player = Players:GetPlayerFromCharacter(hit.Parent)
				local leaderstats = Player:FindFirstChild("leaderstats")
				if not leaderstats then
					return
				end
				local Tokens = leaderstats:FindFirstChild("Tokens")
				if not Tokens then
					return
				end
				SFX.Parent = Player
				SFX:Play()
				SFX.Ended:Connect(function()
					SFX:Remove()
				end)
				Tokens.Value += Worth.Value
				Token:Destroy()
			end
		end)
		RunService.Heartbeat:Connect(function(DT)
			Token:PivotTo(Token.WorldPivot * CFrame.Angles(0, 0, -0.1))
		end)
	end
end

For some reason when I used “math.random” with the baseplate’s size inside it, it’d register as being empty, when the X, Y, and Z bits of the Baseplate size are technically numbers. So, I had to scrap math.random sadly.
EX: (X, Y, Z = Baseplate.Size.X, Baseplate.Size.Y, Baseplate.Size.Z)
Token:PivotTo(Token.WorldPivot * CFrame.new(math.random(-X, X), Y + 12, (math.random(-Z, Z)))

1 Like

Go with a different approach. Shoot raycasts (as many as you want) through out the map, in a downwards direction, and set the part’s position equal to the RayCast.Position. If you’re not sure on how to do this there is a great wiki on create.roblox documentations. This is (probably) a better approach.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.