Attempt to index nil with 'MoveTo'

Here is the script, I don’t know why the error keeps popping up. Any feedback??

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("Arrow")


local ServerStorage = game:GetService("ServerStorage")
local star = ServerStorage:WaitForChild("Star")
local cloneStar = star:Clone()	
cloneStar.Parent = game.Workspace


local function onSpawnStar(player, cloneStar)
	while true do
		wait(.1)
		local level = player.leaderstats.Level.Value
		if game.Workspace.Checkpoints:FindFirstChild(level + 1) then
		local ls = game.Workspace.Checkpoints:FindFirstChild(level + 1)		
			cloneStar:MoveTo(ls.Position + Vector3.new(0,10,0))
		end
		if(player.leaderstats.Level.Value == script.Levels.Value)then
			cloneStar:Destroy()
		end
	end
end


remoteEvent.OnServerEvent:Connect(onSpawnStar)

Try putting a break after cloneStar:Destroy() so it stops the while loop

1 Like

Move these two lines

local cloneStar = star:Clone()	
cloneStar.Parent = game.Workspace

inside of the function

local function onSpawnStar(player, cloneStar)
    local cloneStar = star:Clone()	
    cloneStar.Parent = game.Workspace
    while true do

and definitely add a break after cloneStar:Destroy()

I am gonna try this as soon as my laptop updates

The clone is still not being destroyed. Break isn’t even doing anything;(

I don’t why this is happening I keep trying to figure it out.

Which line is the error occurring on?

You don’t even need a loop for this. Listen for the Object.Changed event, or the Object:GetPropertyChangedSgnal('Property').

I personally use the second one as I don’t need to check what has been changed in order to do certain tasks.

I also recommend using Character:SetPrimaryPartCFrame() instead of :MoveTo()
This will move the instance’s PrimaryPart, which in this case is HumanoidRootPart.
Just make sure to check if the PrimaryPart has been set before moving it.

if Instance.PrimaryPart then
   -- code.
end

SetPrimaryPartCFrame vs MoveTo:
One’s not more reliable to the other, one is just faster. SetPrimaryPartCFrame is the faster option, but MoveTo takes account for collisions.

If you become better with scripting, switch to SetPrimaryPartCFrame because it is faster and if you know your math MoveTo is like something that you don’t really need anymore. (Source: Here.)

2 Likes

It’s not outputting an error, it’s just not working for some reason. Everything else in the script works fine except for the last part. I even tried to change the arguement to

		if(player.leaderstats.Level.Value == 4)then
			cloneStar:Destroy()
			break
		end
	end	
end

4 is the last checkpoint in my game at the moment.

Okay, I am going to test this now. I was thinking about getting rid of the loop for a while. Thanks for the info on MoveTo, I’m definitely going to change it.

1 Like

Try setting the Archivable property of star to true before you clone it

1 Like

Archivable property is set to true.

Try this

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("Arrow")
local ServerStorage = game:GetService("ServerStorage")
local star = ServerStorage:WaitForChild("Star")

local function onSpawnStar(player, cloneStar)
    local cloneStar = star:Clone()
    cloneStar.Parent = game.Workspace
    while true do
        wait(.1)
        local level = player.leaderstats.Level.Value
        if game.Workspace.Checkpoints:FindFirstChild(level + 1) then
            local ls = game.Workspace.Checkpoints:FindFirstChild(level + 1)
            cloneStar:MoveTo(ls.Position + Vector3.new(0,10,0))
        end
        if(player.leaderstats.Level.Value == script.Levels.Value)then
            cloneStar:Destroy()
            break
        end
    end
end

remoteEvent.OnServerEvent:Connect(onSpawnStar)

The Object:GetPropertyChangedSignal('Property') doesn’t make sense to me because I can’t use it with CFrame, I don’t know what other property to put.

I tried this already man, it didn’t work.

Just one quick question I noticed that you have a cloneStar var at the top but you also have a cloneStar var in the function. I know what the one at the top means, but what is cloneStar in the function referring to? I can elaborate if you don’t understand what I’m saying.

Sorry about that :sweat_smile: I actually removed it a while now. I am actually trying to use GetPropertyChangedSignal but I am thinking of using RunService instead. Just don’t know how to implement the RunService as yet.

Okay, that’s fine. If you explain what exactly you’re trying to do I might be able to help with it.

This is as far as I got, it works!!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("Arrow")
local ServerStorage = game:GetService("ServerStorage")
local star = ServerStorage:WaitForChild("Star")


local function onSpawnStar(player)
	local cloneStar = star:Clone()	
	cloneStar.Parent = game.Workspace
	local starloop = true
	
	while true do	
		wait(.1)
		local level = player:WaitForChild("leaderstats").Level.Value
		if game.Workspace.Checkpoints:FindFirstChild(level + 1) then		
			local ls = game.Workspace.Checkpoints:FindFirstChild(level + 1)		
			if cloneStar.PrimaryPart then		
				cloneStar:SetPrimaryPartCFrame(CFrame.new(ls.Position + Vector3.new(0,10,0)))
			end	
		else
			if(level == script.Levels.Value)then
			cloneStar:Destroy()	
			starloop = false
			end
		end
	end
end

	
remoteEvent.OnServerEvent:Connect(onSpawnStar)

Only problem is :sweat: that if I reset my leaderstats value the Star doesn’t come back, how to make it so the Star appears again if you reset your levels and go back to the first checkpoint??

I was talking about checking the stage value when it changes to update the star.