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

moveable block script only moves one block?

I have created a way to move an object using mouse.Target. this is the script

local mouse = game.Players.LocalPlayer:GetMouse()




function moveable()
	mouse.Button1Down:connect(function()
	if mouse.Target.Name == 'moveable' and mouse.Target.Selected.Value == false then
	mouse.Target.Parent = game.Workspace.Selected-- selected is a value in the block called 'moveable'
	mouse.Target.Selected.Value = true--change value to true to notify that its selected
	
	while wait (.001) do
	game.Workspace.Selected.moveable.CFrame = CFrame.new(Vector3.new(mouse.Hit.X,mouse.Hit.Y - .2,mouse.Hit.Z))--change block's cframe to mouse's position.

	end
	end
end)	
end


while true do
	moveable()
	wait()
end

it works, but when i add another "moveable" block, it does not work. it only works for one moveable block. please help?

4 comments

0

Could it possibly be from not reverting the selected value when you deselect a block?

0

no, i have another script to deselect a block. :( any other suggestions?

0

Mabye make another value (selected2?) that functions like selected, but just for the second block.

0

i could do that, and it would work, but i want an efficient thing to use because i want to make many moveable blocks. and i dont want to keep making separate values for all the blocks any other suggestions?.

1 answer

✓ Accepted answer

Well with a more legible version without that useless moveable function...

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:connect(function()
	local target = mouse.Target
	if target and target.Name == 'moveable' and target.Selected.Value == false then
		target.Parent = workspace.Selected
		target.Selected.Value = true
	
		while wait() do
			workspace.Selected.moveable.CFrame = CFrame.new(Vector3.new(mouse.Hit.X,mouse.Hit.Y - .2,mouse.Hit.Z))
		end
	end
end)

I can say its very inefficient to make a new infinite loop each time you make a new selection. You can use another event.

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:connect(function()
	local target = mouse.Target
	if target and target.Name == 'moveable' and target.Selected.Value == false then
		target.Parent = workspace.Selected
		target.Selected.Value = true
	end
end)

mouse.Move:connect(function()
	local part = workspace.Selected:FindFirstChild('moveable')
	if part then
		local pos = mouse.Hit.p
		part.CFrame = CFrame.new(pos.x, pos.y-0.2, pos.z)
	end
end)

This is assuming that your supposed deselection script also works, and there is always only one part inside "Selected". I recommend using an ObjectValue.

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