Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a build tool? (Script already written)

I'm trying to make a build tool for wood blocks. I'd like it to clone a template for a wooden block here's what I have so far (No errors nothing happens, put it in a localscript)

local hitTarget = game.Players.LocalPlayer:GetMouse().Hit
function cloneWood()
	local cloneTemp = game.Workspace.WoodBlock1
	local clonedWood = cloneTemp:Clone()
	clonedWood.Position = Vector3.new(hitTarget)
end

local tool = script.Parent

tool.Equipped:connect(function(Mouse)
	Mouse.Button1Down:connect(function()
		local hitTarget = game.Players.LocalPlayer:GetMouse().Hit
		cloneWood()
	end)
end)

6 comments

0

Where is the script?

0

It's on there.. I put the Lua symbol and the code in the lines..

0

lol I meant in the game, due to line 8 I assume it's in a tool.

0

just to say i feel like you are trying to make a fortnite in roblox e-e

0

No, not fortnite haha

0

I tested my finished script (with mouse.Hit.p) and it worked fine.

1 answer

✓ Accepted answer

Alright, there are a few errors in this script, so let's just go through them one by one.

local hitTarget = game.Players.LocalPlayer:GetMouse().Hit

This line is completely useless, let's just get rid of it.

function cloneWood()
    local cloneTemp = game.Workspace.WoodBlock1
    local clonedWood = cloneTemp:Clone()
    clonedWood.Position = Vector3.new(hitTarget)
end
local tool = script.Parent
tool.Equipped:connect(function(Mouse)
    Mouse.Button1Down:connect(function()
        local hitTarget = game.Players.LocalPlayer:GetMouse().Hit
        cloneWood()
    end)
end)

But, oh no! Now hitTarget isn't defined for the function, and so it will be nil. However, there is a way around this, an arguement!

function cloneWood(hitTarget)
    local cloneTemp = game.Workspace.WoodBlock1
    local clonedWood = cloneTemp:Clone()
    clonedWood.Position = Vector3.new(hitTarget)
end

Now that's good and all, but you never parent clonedWood so let's parent that at then end of function.

function cloneWood(hitTarget)
    local cloneTemp = game.Workspace.WoodBlock1
    local clonedWood = cloneTemp:Clone()
    clonedWood.Position = hitTarget --you also didn't need that Vector3.new()
    clonedWood.Parent = game.Workspace
end

Alright, this is great. We have a nice little function defined, and we're ready to use it! But first, let's take a look at the second half of your script.

local tool = script.Parent
tool.Equipped:connect(function(Mouse)
   Mouse.Button1Down:connect(function()
        local hitTarget = game.Players.LocalPlayer:GetMouse().Hit
        cloneWood()
    end)
end)

I'm not sure why you do line 4 there, you already have the mouse thanks to the first line, let's just clean that up a bit.

local tool = script.Parent
tool.Equipped:connect(function(Mouse)
    Mouse.Button1Down:connect(function()
        local hitTarget = Mouse.Hit
        cloneWood(hitTarget) --and finally, send Mouse.Hit over to the function
    end)
end)

And as a final note,mouse.Hit returns a CFrame value, so you could either send over mouse.Hit.p to get the Vector3, or set the CFrame of your cloned part instead, your choice.

-- and your cleaned script
function cloneWood(hitTarget)
    local cloneTemp = game.Workspace.WoodBlock1
    local clonedWood = cloneTemp:Clone()
    clonedWood.Position = hitTarget
    clonedWood.Parent = game.Workspace
end
local tool = script.Parent
tool.Equipped:connect(function(Mouse)
    Mouse.Button1Down:connect(function()
        local hitTarget = Mouse.Hit
        cloneWood(hitTarget) --once again, mouse.Hit.p or just set the CFrame
    end)
end)

3 comments

0

:connect is deprecateeeeeeed

0

It doesn't matter, it still works.

0

Wait, it still does absolutely nothing, no errors at all.

This discussion is preserved as it was. New answers, comments, and votes are closed.