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

This even won't fire?

I've got this tool going that I retrofitted from a HopperBin. In theory, and by all known laws of RBX.Lua, this script should work, unless I'm overlooking something. The script is below; I've done my best to document it.

s = false --This tells if the tool is selected, used to stop updatePos()

function onSelected(mouse) --Should fire when mouse is selected
	print ("selected")
	s = true --Indicates that the mouse is selected
	t = Instance.new("Part") --From here until line 15 it makes the part
	t.Anchored = true
	t.FormFactor = 2
	t.BottomSurface = "Smooth"
	t.TopSurface = "Smooth"
	t.BrickColor = BrickColor.new(Color3.new(math.random(0,1), math.random(0,1), math.random(0,1)))
	t.Transparency = 0.3
	t.Position = mouse.Hit.p --This is corrected in updatePos()
	t.Size = Vector3.new(4, 0.2, 4)
	t.Parent = game.Workspace
	repeat updatePos(t, mouse) until s == false --Will repeat updatePos() until unselected
	t:Destroy() --Removes part after unselected, as the loop won't run this code until it is done
end

function onDeselected() --Fires when deselcted, destroys the part...
	s = false
end

function updatePos(object, pos) --This one updates the part's position to corespond with the mouse's position, corrected to the nearest stud
	object.Position = Vector3.new(math.ceil(pos.Hit.x), object.Size.Y/2, math.ceil(pos.Hit.z))
	wait(0.0001)
end

script.Parent.Equipped:connect(onSelected) --Fires when equipped
script.Parent.Unequipped:connect(onDeselected) --Fires when unequipped

This script (in HopperBin form) worked exactly as it was supposed to. There are no errors thrown with this, and nothing from here appears in the output.

3 answers

✓ Accepted answer

Tools require a "Handle" part (a Part named Handle parented inside the tool) in order to work (apparently).

I just tested; the script runs fine if you have the handle, and does nothing if you do not. This is probably the problem and absolutely is poor behavior on ROBLOX's part.

This is an irrelevant suggestion, but I suggest that you round the position rather than use ceiling, so that the part ends up more centered!

1 comment

0

Worked! Thanks!

Since the script is in a HopperBin, these events won't work:

script.Parent.Equipped:connect(onSelected)
script.Parent.Unequipped:connect(onDeselected)

Those events only correspond to Tools. Tools aren't HopperBins. They're two completely seperate things. The only similarity is that both are in the backpack. The correct events to use are these:

script.Parent.Selected:connect(onSelected)
script.Parent.Deselected:connect(onDeselected)

I hope this helped!

*Note: For more information on HopperBins, click here: HopperBins

Try getting rid of "s = false" at the beginning?

Also, I'm not sure, but aren't you suppose to indicate what "mouse" is?

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