#I'm trying to generate hex tiles with a hover and selection.

1 messages · Page 1 of 1 (latest)

vagrant notch
#

Hi, I have a script that generates hex tiles and puts a highlight and click detector on the tiles. The script works when I make the hex tile grid with width or height 5 or less but as soon as I increase either the width or height above 5, only some tiles are selectable and does the highlight when the mouse hovers over them, they all still show the hand cursor when I hover the mouse over the tiles. Any help would be much appreciated.

#
print("GenerateHexTileGrid is running!")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HexTemplate = ReplicatedStorage:WaitForChild("HexTile")

local gridWidth = 1
local gridHeight = 2

local tileWidth = 10
local tileHeight = 8.66

local gridOrigin = Vector3.new(14, 0, -14)

for row = 0, gridHeight - 1 do
    for col = 0, gridWidth - 1 do
        local hex = HexTemplate:Clone()
        hex.Name = "Hex_" .. col .. "_" .. row
        hex.Parent = workspace

        -- Force PrimaryPart set
        if not hex.PrimaryPart then
            local part = hex:FindFirstChildWhichIsA("BasePart")
            if part then
                hex.PrimaryPart = part
            else
                warn(hex.Name .. " has no base part!")
                continue
            end
        end
        
        hex.Tile.Anchored = true
        hex.Tile.CanCollide = true
        hex.Tile.CanTouch = true
        
        -- ClickDetector
        local cd = Instance.new("ClickDetector")
        cd.MaxActivationDistance = 100
        cd.Parent = hex.PrimaryPart

        -- Highlight
        local highlight = Instance.new("Highlight")
        highlight.Name = "Highlight"
        highlight.FillColor = Color3.fromRGB(255, 255, 0)
        highlight.OutlineColor = Color3.fromRGB(255, 255, 255)
        highlight.Adornee = hex
        highlight.Enabled = false
        highlight.Parent = hex

        local offsetX = (row % 2 == 0) and 0 or tileWidth / 2
        local x = col * tileWidth + offsetX
        local z = row * (tileHeight * 0.75)
        local worldPos = gridOrigin + Vector3.new(x, 0, z)

        hex:SetPrimaryPartCFrame(CFrame.new(worldPos))
    end
end
#
print("LocalScript is running!")

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

local selectedTile = nil

mouse.Move:Connect(function()
    local target = mouse.Target
    if not target then return end
    
    -- Turn off hover highlights for all other tiles
    for _, obj in workspace:GetChildren() do
        if obj:IsA("Model") and obj:FindFirstChild("Highlight") then
            if obj ~= selectedTile and obj ~= tile then
                obj.Highlight.Enabled = false
            end
        end
    end

    -- Look for a Highlight under the parent model
    local tile = target:FindFirstAncestorWhichIsA("Model")
    if tile and tile:FindFirstChild("Highlight") then
        -- Enable hover highlight if not selected
        if tile ~= selectedTile then
            tile.Highlight.Enabled = true
        end
    end
end)

mouse.Button1Down:Connect(function()
    local target = mouse.Target
    if not target then return end
    local tile = target:FindFirstAncestorWhichIsA("Model")
    if tile and tile:FindFirstChild("Highlight") then
        -- Deselect old tile
        if selectedTile and selectedTile:FindFirstChild("Highlight") then
            selectedTile.Highlight.FillColor = Color3.fromRGB(255, 255, 0)
            selectedTile.Highlight.Enabled = false
        end

        -- Select new tile
        selectedTile = tile
        tile.Highlight.Enabled = true
        tile.Highlight.FillColor = Color3.fromRGB(0, 170, 255)
    end
end)
open gorge
#

lemme see

#

print("LocalScript is running!")

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

local selectedTile = nil
local lastHoveredTile = nil
local hexTiles = {}

for _, obj in workspace:GetChildren() do
if obj:IsA("Model") and obj:FindFirstChild("Highlight") then
table.insert(hexTiles, obj)
end
end

local function disableHighlights(exclude)
for _, tile in ipairs(hexTiles) do
if tile ~= exclude and tile ~= selectedTile and tile.Highlight then
tile.Highlight.Enabled = false
end
end
end

mouse.Move:Connect(function()
local target = mouse.Target
if not target then return end

local tile = target:FindFirstAncestorWhichIsA("Model")
if tile and tile.Highlight then
    if tile ~= lastHoveredTile then
        disableHighlights(tile)
        if tile ~= selectedTile then
            tile.Highlight.Enabled = true
        end
        lastHoveredTile = tile
    end
else
    disableHighlights()
    lastHoveredTile = nil
end

end)

mouse.Button1Down:Connect(function()
local target = mouse.Target
if not target then return end

local tile = target:FindFirstAncestorWhichIsA("Model")
if tile and tile.Highlight then
    if selectedTile and selectedTile.Highlight then
        selectedTile.Highlight.FillColor = Color3.fromRGB(255, 255, 0)
        selectedTile.Highlight.Enabled = false
    end
    
    selectedTile = tile
    tile.Highlight.Enabled = true
    tile.Highlight.FillColor = Color3.fromRGB(0, 170, 255)
end

end)

#

@vagrant notch

#

this work

still loom
vagrant notch
#

Thank you, but this is doing the same. It works with a small grid but as soon as I try to make the grid bigger most tiles don't function as they should. I will see if I can create a video of what happens. The scripts look fine to me but I don't know what could be causing the issue

vagrant notch
lime harnessBOT
#

studio** You are now Level 1! **studio

still loom
#

And didnt yo mama teach you respect

vagrant notch
vagrant notch
still loom
#

🥀

vagrant notch
still loom
vagrant notch
vagrant notch
still loom
vagrant notch
still loom
vagrant notch
still loom
#

well, you have to anyways.

#

and honestly idrk cause im looking at this on my phone, i dont have access to pc rn

#

try renewing the post, maybe other people will help