How To Make a Sandbox Game

This tutorial shows you how to create and make a sandbox-building game.

by EpicArmor1011

Author Avatar

Hello!, You're reading this to learn how to make a sandbox game. Make a zapinator and a placeinator. Its a bit lengthy, but you'll understand it (if you know the basics) First of all, you need to understand how to get the mouse.

GetMouse

To use GetMouse, you have to first either create a variable for the player in a localscript.

local player = game.Players.LocalPlayer

After creating the variable, you can make a new variable; mouse.

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

This unlocks a few new options you can use.

  1. Target

  2. TargetSurface

  3. Hit

  4. Icon

Making The Tools

This is how to make a Zapinator. A tool capable of vaporizing everything that can. Its quite simple. The concept is that it finds the target of the mouse, then deleting the target.

Zapinator:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

mouse.Button1Down:Connect(function()
	if mouse.Target.Locked == false then
		mouse.Target:Destroy()
	end
end)

This is a basic script that deletes parts that aren't locked (meaning you can't delete the baseplate.)

TargetSurface Is used similarly to Target. TargetSurface detects the NormalID of the Target. If one would want to place a block, you would need to first of all find the TargetSurface. Now, this part is optional, but it is useful. If you use mouse.TargetSurface then you could find the NormalID of mouse.TargetSurface - this means that it doesn't place halfway into the part you're trying to build on. Note: this has no grid, therefore its kinda janky.

(no grid) Part of Placeinator:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

mouse.Button1Down:Connect(function()
	local place = Instance.new('Part')
	if mouse.TargetSurface == Enum.NormalID.Top then
		place.Position = Vector3.new(x, y + 1, z)
	elseif mouse.TargetSurface == Enum.NormalID.Bottom then
		place.Position = Vector3.new(x, y - 1, z)
	end
end)

Note that we used mouse.Hit. This find the 3D position of the player's mouse. if you want a grid-like building system then use a math function, I know; math, the worst idea ever made. However it is crucial to make a grid-like building system. This is the example of that grid-like placeinator.

(grid) Part of Placeinator:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

mouse.Button1Down:Connect(function()
	local place = Instance.new('Part')	
	place.Anchored = true
	place.Parent = workspace
	local x = mouse.Hit.Position.X
	local y = mouse.Hit.Position.Y
	local z = mouse.Hit.Position.Z
	if mouse.TargetSurface == Enum.NormalID.Top then
		place.Position = Vector3.new(math.round(x), math.round(y) + 1, math.round(z))
	elseif mouse.TargetSurface == Enum.NormalID.Bottom then
		place.Position = Vector3.new(math.round(x), math.round(y) - 1, math.round(z))
	end
end)

This script segment only works if its in a localscript and you click either the top or bottom surfaces of a part.

So, you might be wondering why the grid is a gigachadzillion times longer, It has to do with the normalId. there might be a ton of different easier ways to do this but I dont know those. If you do, feel free to shorten the code. If however you wanted to make a grid in terms of numbers you can create a new variable and name it "grid". You can set grid to any number. the number you put can represent the grid. For example; you could set grid to 2. and this wouldn't do anything but it would change grid to 2 (obviously) so, to make the grid actually work, you need to divide the coordinate values, then multiply them.

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

local grid = 2

mouse.Button1Down:Connect(function()
	local place = Instance.new('Part')	
	place.Anchored = true
	place.Parent = workspace
	local x = mouse.Hit.Position.X
	local y = mouse.Hit.Position.Y
	local z = mouse.Hit.Position.Z
	if mouse.TargetSurface == Enum.NormalID.Top then
		place.Position = Vector3.new(math.round(x/grid)*grid, math.round(y/grid)*grid + 1, math.round(z/grid)*grid)
	elseif mouse.TargetSurface == Enum.NormalID.Bottom then
		place.Position = Vector3.new(math.round(x/grid)*grid, math.round(y/grid)*grid - 1, math.round(z/grid)*grid)
	end
end)

Boom, The Placeinator. Places the standard grey brick wherever your mouse is. Also only one of these finished scripts can work. Say you wanted to add textures to the blocks, its easy. Create an Instance.new, Nevermind. It's better if i show you the script in code.

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

mouse.Button1Down:Connect(function()
	local place = Instance.new('Part')
	if mouse.TargetSurface == Enum.NormalID.Top then
		place.Position = Vector3.new(x, y + 1, z)
	elseif mouse.TargetSurface == Enum.NormalID.Bottom then
		place.Position = Vector3.new(x, y - 1, z)
	end
	local texture = Instance.new("Texture")
	texture.Parent = place
	texture.TextureID = 0 --change this to be your texture ID.
end)

I know, you're tired, but there is one more thing that you can edit. The size. It's surprisingly simple. Create a variable called size, and change place's size to Vector3.new(size, size, size)

local player = game.Players.LocalPlayer

local mouse = player:GetMouse

local size = 2

mouse.Button1Down:Connect(function()
	local place = Instance.new('Part')
	if mouse.TargetSurface == Enum.NormalID.Top then
		place.Position = Vector3.new(x, y + 1, z)
	elseif mouse.TargetSurface == Enum.NormalID.Bottom then
		place.Position = Vector3.new(x, y - 1, z)
	end
	place.Size = Vector3.new(size, size, size)
end)

And your done. You made a The Placeinator and The Zapinator. You might need a few bug fixes but once you fix everything, you should be good to go!

View in-game to comment, award, and more!