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

Model moving script malfunctional?

I made a script that makes a noise and moves a model and whatnot, but when it is activated, the model stays put. The output does not tell me anything related to this script. Here is the script.

function separate()
if game.Workspace:FindFirstChild("Stardrive")== nil then
    script.Parent.Sound.Pitch = 1
    script.Parent.Sound:play()
    wait(3)
    local s = game.ServerStorage.Stardrive:clone()
    s.Parent = game.Workspace
    game.Workspace.Separated.Value = true
    wait(0.5)
    local p = game.Workspace.Stardrive:GetChildren()
    repeat
    wait(0.5) 
    for i,v in pairs(p) do
        v.CFrame = v.CFrame + Vector3.new(0, 0, 10) 
    end
    until 
	game.Workspace.Stardrive.Deflector.Position.Z <= 1728.95
else
    if game.Workspace:FindFirstChild("Stardrive")~= nil then
        script.Parent.Sound.Pitch = -1
        script.Parent.Sound:Play()
        game.Workspace.Separated.Value = false
        game.Workspace.Stardrive:destroy()
    end
end
end

script.Parent.ClickDetector.MouseClick:connect(separate)

Anyone know why it won't work?

1 answer

✓ Accepted answer

The reason it's probably not moving is because of the generic for loop. These loops only process one object at a time, so it's moving all the parts one by one, if the model has a lot of parts this might be your problem. If you want to move a model try using :SetPrimaryPartCFrame(). It changes the cframe of that part and all the parts follow it so it's moving a whole model at once. You might want to measure how many intervals it takes to get from it's original position to the position you want it to go, so it would be more efficient than saying game.Workspace.Stardrive.Deflector.Position.Z <= 1728.95.


#SetPrimaryPartCFrame(CFrame) This teleports the primary part of a model, make sure you set the primary part. In this case maybe you would set the PrimaryPart to be the Deflector.

Model:SetPrimaryPartCFrame(CFrame.new(0,0,10))


#Final Product

function separate()
if game.Workspace:FindFirstChild("Stardrive")== nil then
    script.Parent.Sound.Pitch = 1
    script.Parent.Sound:play()
    wait(3)
    local s = game.ServerStorage.Stardrive:clone()
    s.Parent = game.Workspace
    game.Workspace.Separated.Value = true
    wait(0.5)
    for i = 1,10 do --Replace 10 to how many times you want this to loop to get to the right position.
	s:SetPrimaryPartCFrame(s.PrimaryPart.CFrame+Vector3.new(0,0,10))
    end
else
    if game.Workspace:FindFirstChild("Stardrive")~= nil then
        script.Parent.Sound.Pitch = -1
        script.Parent.Sound:Play()
        game.Workspace.Separated.Value = false
        game.Workspace.Stardrive:Destroy()
    end
end
end

script.Parent.ClickDetector.MouseClick:connect(separate)

Hope it helps!

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