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

How can I move a model when I click an object?

When I ring the doorbell I want my NPC to be able to change the position to the ones I have in the code but this isn't working. I think I need to use the Torso, however the NPC is just a model without a humanoid so I would need to move the whole model. << Probably overthinking this as well

wait()
local npc = game.Workspace.House1.NPC -- Needs Fixing
local doorbell = game.Workspace.House1.Doorbell
local dbs = game.ServerStorage.Sounds.Doorbell_Sound
local Window = game.Workspace.House1.Windows
deb = true 
script.Parent.Doorbell.ClickDetector.MouseClick:connect(function(wat)
	if deb == true then
		deb = false
			dbs:Play()
			Window.BrickColor = BrickColor.new("Bright yellow")
			Window.Transparency = .45
			Window.Material = "Neon" 
			npc.CFrame = CFrame.new(Vector3.new(-38.4, 4.5, 18.8)) -- Want NPC to teleport here (Outside House)
			wait(5)
			npc.CFrame = CFrame.new(Vector3.new(-38.4, 4.5, 24.9)) -- Want NPC to teleport here (Inside House)
			Window.BrickColor = BrickColor.new("Black")
			Window.Transparency = 0
			Window.Material = "SmoothPlastic"
			deb = true
	end
end)

2 comments

1

You would need to use npc.Torso.CFrame, not npc.CFrame.

0

Also, you don't need to give a vector to a CFrame. You can just give the numbers directly.

2 answers

As far as I know, you have to script all the parts inside the NPC to go to their respective locations. If your NPC is a Humanoid tho, you can probably just set the torso's CFrame and all the limbs would go along. Probably, I don't know.

The way I move blocks is by Part Sliding

I have been working on a Factory Tycoon type game and I have recently figured out how to move a block through any activation methods such as Click or Touch (there are plenty more but we'll keep this simple)

In order to make a block move due to click you need:

  1. A part you want to move
  2. A block you will use that will activate the function
local Part = Workspace.Part -- this is the Part we will move
local newPos = Part.Position + Vector3.new(0,5,0) -- the position the Part will go to
local Time = 10 -- the time that the script will take to move the part
local Increment = 0.5 -- the Part will move 0.5 studs each time it moves
local Debounce = false
 
local Diff = newPos - Part.Position -- the difference between the two positions
local Mag = Diff.magnitude -- the distance between the two parts
local Direction = CFrame.new(Part.Position, newPos).lookVector
 
function MovePart() -- function to move the Part
	if Debounce then return end -- end the function if debounce is true
	Debounce = true -- make Debounce true so the function can't run
	for n = 0, Mag, Increment do
		Part.CFrame = Part.CFrame + (Direction * Increment)
		wait( (Time/Mag) * Increment )
	end
	Debounce = false -- set Debounce to false so the function can run again
--THIS SECTION IS ONLY IF YOU WANT TO BRING PART BACK TO ORIGINAL POSITION
	--wait(5) -- waits 5 seconds before resetting position
	-- ResetPart() -- Activates ResetPart() function
-- END OF RESET SECTION
end
 
Workspace.Button.ClickDetector.MouseClick:connect(MovePart) --On click activate MovePart (and path of button part

Along with that I had created the inverse of the local variables presented in the beginning of the script and made a new function to RESET PART POSITION: (insert under other local variables stated in beginning of script)

local RnewPos = Part.Position + Vector3.new(0,-5,0) -- The Inverse position of newPos
 local RDiff = RnewPos - Part.Position -- the difference between the two positions
local RMag = RDiff.magnitude -- the distance between the two parts
local RDirection = CFrame.new(Part.Position, RnewPos).lookVector

(insert under MovePart()

function ResetPart() -- function to reset the Part to its original position
	if Debounce then return end -- end the function if debounce is true
	Debounce = true -- make Debounce true so the function can't run
	for n = 0, RMag, Increment do
		Part.CFrame = Part.CFrame + (RDirection * Increment)
		wait( (Time/RMag) * Increment )
	end
	Debounce = false -- set Debounce to false so the function can run again
end

2 comments

0

Sorry if its a bit to read x) but you know, i'd rather over explain it than leave things unexplained

0

ALSO: Referring to your problem with moving an NPC, if you have it grouped together you can replace " line 1: Local Part = workspace.Part " with one that moves your grouped parts to be "line1: local Part = workspace.NPC

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