Attempt to call a nil value

Okay, so i’m trying to make a grid for a building tool. I’m new at coding, and I can’t find a way to apply the actual grid. I tried a lot of things, like making the location from local script be grid-iffied first, but that didn’t work either. Not sure what to do.
ServerScript:

local tool = script.Parent

local clickEvent = game.ReplicatedStorage:WaitForChild('BlockPlace')

local clickEventConnection

-- Partval = tool.PartNum.Value

function griddify(locationy)

return math.floor(locationy / 5) * 5

end

local function createPart(location, griddify)

local part=tool.Part0:Clone()

part.CanCollide=true

part.Transparency = 0

part.CFrame = (griddify(location))

script.Parent.Click:Play()

part.Parent = workspace

part.Anchored=true

part.Orientation=Vector3.new(0,0,0)

end

function onClick(player, clickLocation)

createPart(clickLocation)

end

local function onEquip()

clickEventConnection = clickEvent.OnServerEvent:connect(onClick)

end

local function onUnequip()

clickEventConnection:disconnect()

end

tool.Equipped:connect(onEquip)

tool.Unequipped:connect(onUnequip)

LocalScript:

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local clickEvent = game.ReplicatedStorage:WaitForChild('BlockPlace')

--round(mouse.Hit.Position,3)
local function onActivate()
    local clickLocation = mouse.Hit
	clickEvent:FireServer(clickLocation)
end
 
tool.Activated:connect(onActivate)
1 Like

When defining your CreatePart function, remove the second parameter, “griddify”. Since you are not passing in a value for it, it is going to be nil. Since the parameter has the same name as the function, when you call it later, the script might take the nil value instead of the griddify() function.

2 Likes

But then I get another error: attempt to perform arithmetic (div) on CFrame and number

2 Likes

Are you trying to divide the Y position by 5?

2 Likes

Inside your LocalScript, you are sending the Hit CFrame instead of the Hit.Position Vector3 as clickLocation. Try setting clickLocation to mouse.Hit.Position.

3 Likes

How would I fix this? I thought the grid system was for the CFrame? I just used a grid system from another forum post, pasted it there and tried to mix it with my code to work properly.

1 Like

Hold on. Let me modify the code, and then DM it to you.

1 Like