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

How Should I Make a Grid Building System?

Asked by 4 years ago

A feature in my game is that you can build stuff like walls, floors, etc. I've never done this before so how could I approach this problem?

I've already tried this tutorial: https://scriptinghelpers.org/blog/creating-a-furniture-placement-system but it doesn't appear to work (like I can't get anything to show up, and it doesn't even print any errors).

I would like items to stack on top of each other and snap to a grid, along with showing a preview of the item when you drag it around. They shouldn't overlap with each others' hitboxes. I have several types of blocks that aren't the same dimensions but are all integers. The plot I'm working with is invisible with collisions off.

Just for simplicity let's pretend that I have a single type of item that you can build with by clicking on the screen and right-clicking to remove. I can figure out how to implement building with other items later. I can also figure out a save system later, so no need to help me with that right now.

I know this might seem like me asking you to make a whole game for me but I really just want some tips, helpful links, etc. since I couldn't find anything useful. I would highly appreciate a few code snippets helping me go in the right direction.

Let me know if my question is confusing or too vague.

Thanks for your time :)

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

what you want to do is clone the item you want to build, put it in the workspace and make it go to the mouse.hit.p until the left mouse button is clicked. you will need a localscript to detect if the player is currently building, and to communicate with the script so the building can move accordingly and also to fire an event upon mouse click to place the build down.

code sample from my game:

script.Parent.HOLDER2.BuildInfo.Build.BuildButton.MouseButton1Click:Connect(function()
    if script.Parent.HOLDER2.BuildInfo.Build.BuildButton.Text == "Build" and (game.ReplicatedStorage.CurrentlyBuilding.Value) == false then
        game.Workspace.SoundFX.Tap:Play()
        game.ReplicatedStorage.AttackHappening.Value = false
        local itemtobuild = game.ServerStorage.Buildings:FindFirstChild(script.Parent.HOLDER2.BuildInfo.SelectedBuilding.Value)["1"]
        game.ReplicatedStorage.CurrentlyBuilding.Value = true
        local build = itemtobuild:Clone()
        build.Parent = game.Workspace
        local move
        local click
        move = game.ReplicatedStorage.UpdateBuildPosition.OnServerEvent:Connect(function(plr, pos)
            build:SetPrimaryPartCFrame(CFrame.new(math.floor(pos.X/15) * 15, 2.25, math.floor(pos.Z/15) * 15))
        end)
        click = game.ReplicatedStorage.SetBuildInPosition.OnServerEvent:Connect(function(plr, pos)
            build:SetPrimaryPartCFrame(CFrame.new(math.floor(pos.X/15) * 15, 2.25, math.floor(pos.Z/15) * 15))
            move:Disconnect()
            game.ReplicatedStorage.CurrentlyBuilding.Value = false
            click:Disconnect()
        end)
        script.Parent.Enabled = false
        script.Parent.Parent.BuildUI.Enabled = true
        repeat
            wait()
        until game.ReplicatedStorage.CurrentlyBuilding.Value == false
    end
end)

hope this helps a bit.

0
Thanks! I'll go try it out Bob4koolest 78 — 4y
Ad

Answer this question