I'm trying to make a mouse based tool that builds things. I've figured out how to do everything (check for enough funds, create a stored model on the button click) But I cant really figure out how to show a preview of the object being built (like a .5 transparent "ghost") or how to check if there's space for the model to be placed.
I'm using a grid system so I can force the objects to be built in a defined space but I need a solution that either:
OR
^9/8/2015
V 9/9/2015
Ok so I decided to go a different route. Im going to use the magic of the "target" builtinfunction to reference each tile.
This has the benefit of not having to keep a massive table of data and i can keep track of whether a current tile is selected or not. I started coding and got real excited when I started to get a system that worked here it is:
Bin = script.Parent hasoldtarget = false oldtarget = nil function onButton1Down(mouse) target = mouse.Target if target.Name == "Plot" then target.BrickColor = BrickColor.new(21) if hasoldtarget == false then target.BrickColor = BrickColor.new(22) oldtarget = target hasoldtarget = true return end if hasoldtarget == true and oldtarget ~= target then oldtarget.BrickColor = BrickColor.new(0) target.BrickColor = BrickColor.new(24) oldtarget = target print(oldtarget) return end if hasoldtarget == true and oldtarget == target then oldtarget.BrickColor = BrickColor.new(0) target.BrickColor = BrickColor.new(0) oldtarget = target return end end end function onButton2Down(mouse) target = mouse.Target if target.Name == "Plot" then target.BrickColor = BrickColor.new(23) end end function onSelected(mouse) mouse.Icon = "rbxasset://textures\\GunCursor.png" mouse.Button1Down:connect(function() onButton1Down(mouse) end) mouse.Button2Down:connect(function() onButton2Down(mouse) end) end Bin.Selected:connect(onSelected)
IT WORKS!!!...BUT ONLY IN THE STUDIO!!! fml...
Quick rundown of how code works. I'm essentially trying to keep track of what tile is being selected. The code looks at the target clicked and stores it. It then changes the color of the tile based on whether or not it is a tile (called 'em "plots") from last click or a new one. The colors are as follows
Pink(22) = the first selected tile (player is using the tool for the first time so there is no stored previous)
Yellow(23)= The current selected tile (I plan to add building functionality with the right click and all it will need to do is check if the "plot"color is yellow)
Grey(0)=A previously selected, but now un-selected tile (Selecting the same plot twice will de-select)
I thought it was a pretty lightweight solution but for WHATEVER reason the code doesn't run on an actual server. The gun cursor wont even load (mouse.Icon = "rbxasset://textures\GunCursor.png")
You have a couple options:
function ToGridCoordinates(v) return Vector2.new(math.floor(v.X / 10), math.floor(v.Z / 10)) end function ToWorldCoordinates(v) --returns centre of grid square return Vector3.new(v.X * 10 + 5, 0, v.Y * 10 + 5) end
With those functions, each square is 10 pixels wide, and Square (0, 0) ranges from (0,0,0) up to but not including (10, 0, 10)