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)
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.)
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.
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)
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 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.
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 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??