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.
#I'm trying to generate hex tiles with a hover and selection.
1 messages · Page 1 of 1 (latest)
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)
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
dont spoonfeed
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
Why are you here? This is very helpful. No one is looking for yo empty spoon
** You are now Level 1! **
The channels policy is that we do not make scripts for people. Doing so anyway is called spoonfeeding.
And didnt yo mama teach you respect
Did I ask for a script? I'm asking for help and your comment was not very helpful. I am asking what could be wrong. Point me in some direction so I can go look in to it
Same can be applied to your comments
Dude this aint even abt u bruh
🥀
Lol, I'm sorry, I'm just looking for help. I'm new to Roblox dev but not new to coding. It look right to me so maybe it's not my scripts then?
Try deleting continue from the else statement
I removed the else statement, but it is still doing the same
No the continue part, not the else syatement
I added the else with the warn() back, but its only a output print and I have checked the output and don't see any warnings
are you sure the local script is running? also why are you defining the tile variable after you use it?
Yea, I was playing around with it, swapping those 2 blocks of code around, trying to find the problem before posting on here. I did move the tile declaration to the top, but still does the same 🙈