Why is this checkpoint script not working?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a checkpoints script so that when the player touches the stage part it saves it as their death spawn to respawn
  2. What is the issue? Include screenshots / videos if possible!
    the checkpoint only saves at the first checkpoint the player touches
    The output error are Players.Diamondultra123.PlayerScripts.LocalScript:21: attempt to index nil with number
    Also any possibles improvements to the script is appreciated
Module Script 

local module = {}

function module.set(plr,arrow,stage,plrstage,stagenum,pressed)
	
	if plrstage.Value < stagenum  then
		
		print(plrstage)
		print(stagenum)
		plrstage.Value = stagenum
		print(plrstage)
		print("this passed")
		stage.CHECK.BrickColor = BrickColor.new("Lime green")
		pressed = true
		print(plr)
		return {plr,stage,pressed}
		
	end

        
end




--------------------------------------------------------
Local SCRIPT
local mod = require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))

local plr = game.Players.LocalPlayer
local checkpointfolder = game.Workspace.Checkpoints
local pressed = false

local spawnpad = game.Workspace.SpawnLocation

  
for i, v in pairs(checkpointfolder:GetChildren()) do
	v.Touched:Connect(function(hit)
		print("this got touched")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local plrstage = player.leaderstats:WaitForChild("Stage")
		local stage = v
		local arrow = game.Workspace.Arrow
		local stagenum = v.Value.Value
		plr.RespawnLocation = spawnpad
	
	 local data = mod.set(player,arrow,stage,plrstage,stagenum,pressed)
		print(data[2]:GetChildren())
		if data[2].Value.Value == data[1]:WaitForChild("leaderstats").Stage.Value then

			spawnpad.Position = data[2].Position + Vector3.new(0,.3,0)
			data[1].CharacterAdded:Connect(function(char)
				char:MoveTo(data[2].Position) 
                  repeat task.wait()
					until char ~= nil
				print(data[2])
                print(char.PrimaryPart.Position)
				char:MoveTo(data[2].Position) 
					pressed  = false

			end)
		
	
        end   

	end)
	
	
	
	
end
1 Like

You can set a player’s respawn point by doing the following:

local function SetPlayerRespawn(Player: Player, RespawnPoint: SpawnLocation)
    local Character = Player.Character or Player.CharacterAdded:Wait()

    if Character then
        local Humanoid = Character:FindFirstChild("Humanoid")

        if Humanoid then
            Humanoid.RespawnLocation = RespawnPoint
        end
    end
end
1 Like

Can you please explain the use of colon please in parameters because I don’t fully understand it

1 Like

Colons pass the table you called the method on as the first parameter:

function Object:Method(arg1, arg2)
-- Parameter line up: (Object, arg1, arg2)
-- Even though `self` isn't defined in the parameters, it's passed to the function's scope automatically (implicitly defined)

function Object.Method(arg1, arg2)
-- Parameter line up: (arg1, arg2)
-- The object wasn't passed nor is `self` accessible here

function Object.Method(self, arg1, arg2)
-- Parameter line up: (Object, arg1, arg)
-- This is the same as the first example except it uses a dot and explicitly declares `self`

For more: What is self and how can I use it? - #2 by WallsAreForClimbing

Edit: Misunderstood the question, but I’ll leave this up!

1 Like

The colons are used to signify what type of object the parameter should be

1 Like

can it be a custom object like a custom variable or only the ones provided by Roblox?

1 Like

Only the ones provided by ROBLOX

function printThing(string: String)
     print(string)
end

Also, it is used to help indicate what to use in function parameters

1 Like

so is the first word “string” a variable for the “:String”

1 Like

Maybe, type indications dont really matter anymore
This:

function printThis(str)
    print(str)
end

and this:

function printThis(str: String)
    print(str)
end

they are the same thing

1 Like

okay, thank you for the clarification

You can actually pass custom types:

type FileType = "png" | "jpeg" | "mp4"
type FileData = { name: string, content: string }

local function openFile(fileType: FileType) : FileData
   --...
end

(This would also autocomplete the string list for the argument)

image