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

Why does this script only work in the studio?

Asked by 8 years ago

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:

  1. Checks that the model is indeed being placed in the center of a grid square (10X10 squares with 1 stud borders between each plot) |____|____|____|

OR

  1. Have the previews snap to each grid square

^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")

0
How are you storing what buildings take up each grid 'tile'? adark 5487 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

You have a couple options:

  1. Don't use a grid system; let the buildings be placed where-ever. If, when trying to place the building, you use :MoveTo and the building does not end up where you expect it to (ie its Y position is higher than expected), then something is in the way. This way is a bit inflexible, though - a player standing in the wrong spot will prevent building placement, and it's difficult to make work if your terrain isn't perfectly flat.
  2. Use a grid system. Either keep track of what's "on the board" with a 2 dimensional table (ie each grid space on the table would refer to what's on it), or simply keep a collection of buildings and their associated coordinates. You need to have a way to convert between world coordinates and grid coordinates. Ex, you might use:
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)

Ad

Answer this question