I’ve been trying to make a checkpoint system, and after following a tutorial, I can up with this code:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue")
Level.Parent = leaderstats
Level.Name = "Level"
Level.Value = 1
player.CharacterAdded:Connect(function(character)
wait()
local checkpoint = game.Workspace.Checkpoints:FindFirstChild("Checkpoint" .. Level.Value)
end)
end)
for i, v in pairs (game.Workspace.Checkpoints:GetChildren()) do
v.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local Level = player.leaderstats.Level
local CheckpointNumber = tonumber(string.sub(v.name, 11, #v.name))
if CheckpointNumber > Level then
Level.Value = CheckpointNumber
end
end
end)
end
However, it keeps giving me an error on line 23 (if CheckpointNumber > Level then), saying that I cannot compare a number to an Instance. There doesn’t seem to be an error on the tutorial, did I do something wrong?
You need a .Value when trying to get the value of anything
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue")
Level.Parent = leaderstats
Level.Name = "Level"
Level.Value = 1
player.CharacterAdded:Connect(function(character)
wait()
local checkpoint = game.Workspace.Checkpoints:FindFirstChild("Checkpoint" .. Level.Value)
end)
end)
for i, v in pairs (game.Workspace.Checkpoints:GetChildren()) do
v.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local Level = player.leaderstats.Level.Value
local CheckpointNumber = tonumber(string.sub(v.name, 11, #v.name))
if CheckpointNumber > Level then
Level.Value = CheckpointNumber
end
end
end)
end
Thts because u referenced value once again after declaring it. Heres wt I mean:
local Level = player.leaderstats.Level.Value --here u declared .Value
local CheckpointNumber = tonumber(string.sub(v.name, 11, #v.name))
if CheckpointNumber > Level then
Level.Value = CheckpointNumber -- u declared .Value again
Replace it with this
local Level = player.leaderstats.Level
local CheckpointNumber = tonumber(string.sub(v.name, 11, #v.name))
if CheckpointNumber > Level.Value then
Level.Value = CheckpointNumber
local players = game:GetService("Players")
local checkpoints = workspace:WaitForChild("Checkpoints")
players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue")
Level.Parent = leaderstats
Level.Name = "Level"
Level.Value = 1
player.CharacterAdded:Connect(function(character)
local checkpoint = checkpoints:FindFirstChild("Checkpoint" .. Level.Value)
end)
end)
for i, v in ipairs(checkpoints:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit:FindFirstChild("Humanoid") then
local char = hit.Parent
local plr = players:GetPlayerFromCharacter(char)
local leaderstats = plr:WaitForChild("leaderstats")
local level = leaderstats:WaitForChild("Level")
local CheckpointNumber = tonumber(string.sub(v.Name, 11, #v.Name))
if CheckpointNumber > level.Value then
level.Value = CheckpointNumber
end
end
end)
end
end