#my script ins't working how i want

1 messages · Page 1 of 1 (latest)

keen heart
#

i made this script to place blocks, but for some reason, the blocks are being placed inside each other when i click in some directions, what i did wrong?

local Hammer = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local function roundToGrid(number)
return math.round(number / 2) * 2
end

local function createBlock(position)
local newPart = Instance.new("Part")
newPart.Size = Vector3.new(2, 2, 2)
newPart.Material = Enum.Material.WoodPlanks
newPart.Color = Color3.new(0.603922, 0.447059, 0.294118)
newPart.Anchored = true
newPart.Position = position
newPart.Parent = workspace
end

Hammer.Activated:Connect(function()
local mousePos = mouse.Hit.Position
local gridX = roundToGrid(mousePos.X)
local gridY = roundToGrid(mousePos.Y)
local gridZ = roundToGrid(mousePos.Z)

print("Posição do Mouse:", mousePos)
print("Grid X:", gridX, "Grid Y:", gridY, "Grid Z:", gridZ)

local gridPosition = Vector3.new(gridX, gridY, gridZ)
createBlock(gridPosition)

end)

sacred marlinBOT
#

studio** You are now Level 4! **studio

serene narwhal
#

It's because when you're placing blocks, your mouse is exactly along the grid which causes the rounding to go only one way. So you dont know what face you clicked. Like if you clicked the top face of a block at (0,0,0), its the same as clicking the bottom face of a block at (0,2,0).

#

I think you can use workspace:Raycast() along with workspace.CurrentCamera:ScreenPointToRay() to find the normal of what you clicked, and add the normal as an offset before rounding to the grid

ruby sigil
#

I havent been in scripting help channels in a while. Now that ChatGPT exists, u can fix this stuff pretty fast

#

I copied and pasted the problem into ChatGPT, and it had the same rationale but it didnt know how to find the normal properly without guidance

tranquil ridge
#

You have to have some sense when using chat gpt. It gets things wrong... Like a lot

ruby sigil
#

Its sort of funny, i think web dev is easier now due to how much training LLMs have on it

#

Easier than Roblox dev that is*

#

It’s horrible still with some Roblox API stuff since stuff like constraint movers have an unorthodox way of working logically i guess

sacred marlinBOT
#

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

tranquil ridge
#

Yeah

ruby sigil
#

Maybe you can prompt chatgpt with the correct info though to code constraint movers properly

keen heart
#

i was struggling to solve this problem

serene narwhal
keen heart
serene narwhal
#

sure

keen heart
#

idk how to make that

serene narwhal
#

This code just replaces using Mouse.Hit

...

Hammer.Activated:Connect(function()
  local range = 200 -- max raycast distance
  local ray = workspace.CurrentCamera:ScreenPointToRay(Mouse.X, Mouse.Y) -- create the ray from your mouse position
  local params = RaycastParams.new() -- create raycasting parameters
  params.FilterType = Enum.RaycastFilterType.Exclude
  params.FilterDescendantsInstances = {LocalPlayer.Character} -- set to include ignoring your character when raycasting
  local result = workspace:Raycast(ray.Origin, ray.Direction * range, params)
  if not result.Instance then return end -- stop if they raycast didn't hit anything
  
  -- now continue your existing code here
  local mousePos = result.Position
  local mouseNormal = result.Normal
  local gridX = roundToGrid(mousePos.X + mouseNormal.X * 0.1) -- add the normal offset of the target face
  local gridY = ...
  ...
end)
serene narwhal
#

np let me know if it works

keen heart
#

i was like 6-8 hours trying to fix it

keen heart
serene narwhal
#

are there any errors

keen heart
#

i think im so dumb for that bro

serene narwhal
#

its ok, can i see ur current code?

keen heart
#

ok

#

local Hammer = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local function roundToGrid(number)
return math.round(number / 2) * 2
end

local function createBlock(position)
local newPart = Instance.new("Part")
newPart.Size = Vector3.new(2, 2, 2)
newPart.Material = Enum.Material.WoodPlanks
newPart.Color = Color3.new(0.603922, 0.447059, 0.294118)
newPart.Anchored = true
newPart.Position = position
newPart.Parent = workspace
end

Hammer.Activated:Connect(function()
local range = 200 -- max raycast distance
local ray = workspace.CurrentCamera:ScreenPointToRay(Mouse.X, Mouse.Y) -- create the ray from your mouse position
local params = RaycastParams.new() -- create raycasting parameters
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {LocalPlayer.Character} -- set to include ignoring your character when raycasting
local result = workspace:Raycast(ray.Origin, ray.Direction * range, params)
if not result.Instance then return end -- stop if they raycast didn't hit anything

-- now continue your existing code here
local mousePos = result.Position
local mouseNormal = result.Normal
local gridX = roundToGrid(mousePos.X + mouseNormal.X * 0.1) -- add the normal offset of the target face
local gridY = ...
...

end)

serene narwhal
#

oh you have to finish writing the code at the bottom

keen heart
#

oh, with my previous code?

serene narwhal
#

the local gridY = ... was to fill in basically what you had before

#

yes, you also need to add mouseNormal.Y * 0.1, and mouseNormal.Z *0.1 respectively like i did with gridX

keen heart
#

uh

#

yeah, im too dumb to make codes

serene narwhal
#

thats ok ill fill it in

keen heart
#

another failed project

#

i can't even make a hammer place a block

serene narwhal
#
local Hammer = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local function roundToGrid(number)
    return math.round(number / 2) * 2
end

local function createBlock(position)
    local newPart = Instance.new("Part")
    newPart.Size = Vector3.new(2, 2, 2)
    newPart.Material = Enum.Material.WoodPlanks
    newPart.Color = Color3.new(0.603922, 0.447059, 0.294118)
    newPart.Anchored = true
    newPart.Position = position
    newPart.Parent = workspace
end

Hammer.Activated:Connect(function()
    local range = 200 -- max raycast distance
    local ray = workspace.CurrentCamera:ScreenPointToRay(Mouse.X, Mouse.Y) -- create the ray from your mouse position
    local params = RaycastParams.new() -- create raycasting parameters
    params.FilterType = Enum.RaycastFilterType.Exclude
    params.FilterDescendantsInstances = {LocalPlayer.Character} -- set to include ignoring your character when raycasting
    local result = workspace:Raycast(ray.Origin, ray.Direction * range, params)
    if not result.Instance then return end -- stop if they raycast didn't hit anything
    
    -- now continue your existing code here
    local mousePos = result.Position + result.Normal * 0.1  -- add the normal offset of the target face
    local gridX = roundToGrid(mousePos.X)
    local gridY = roundToGrid(mousePos.Y)
    local gridZ = roundToGrid(mousePos.Z)

    local gridPosition = Vector3.new(gridX, gridY, gridZ)
    createBlock(gridPosition)
end)
keen heart
#

i will try

#

but im about to give up

#

yeah, didn't work again

serene narwhal
#

can you see what errors are printing?

keen heart
#

ok, let me see

serene narwhal
#

in the output?

#

ok

keen heart
serene narwhal
#

oh right. i think your mouse variable has a lowercase m not uppercase

#

change "Mouse" to "mouse" and "LocalPlayer" to "player"

#

in the script, they should have red lines under them indicating they are incorrect

keen heart
#

ok

#

IT WORKED

#

FINALLY

#

@serene narwhal TYSM BRO

#

ur my hero

serene narwhal
#

nice good that its working now

keen heart
#

i willl give you credits when the game release

serene narwhal
#

oh thank you

keen heart
#

here is

serene narwhal
tough solar
#

Chat GPT is the last thing to use unless you simply ask a question

#

AI generating is brain rotting the new scripters

#

Stay away from that shit

#

At all costs

keen heart
#

a pretty usefull wall, but still like a wall

ruby sigil
#

Since web development can almost be completely automated now