Answered by
6 years ago Edited 6 years ago
GENERAL PRACTICE
Use the :GetService()
method to check for the "ServerStorage" and "Players" rather than immediately calling them.
You don't require the tostring()
on the UserId since the numbers can be directly taken by the script.
Considering how this code is written, it's probably better to separate the .Touched
and .CharacterAdded
events since you will have the character added before touching a part.
ISSUES
connect
is deprecated in favor of Connect
The actual Property name for a player's id is UserId
, not "userId"
Use the :SetPrimaryPartCFrame()
of models (the character is a model) to move the player since just moving their rootpart may sometimes cause the player's body to remain where it is and only move the one part.
spawn
is a function, so consider changing the name of this variable (my example switches it to "sp")
REVISED SERVER SCRIPT
01 | game:GetService( "Players" ).PlayerAdded:Connect( function (player) |
02 | local objval = Instance.new( "ObjectValue" ) |
03 | objval.Name = "CheckpointData" |
05 | objval.Parent = player |
07 | player.CharacterAdded:Connect( function (character) |
08 | repeat wait() until character:FindFirstChild( "HumanoidRootPart" ) ~ = nil |
09 | if objval.Value ~ = nil then |
10 | character:SetPrimaryPartCFrame(objval.Value.CFrame + Vector 3. new( 0 , 4 , 0 )) |
15 | for w, sp in pairs (workspace:WaitForChild( "Checkpoints" ):GetChildren()) do |
16 | sp.Touched:Connect( function (hit) |
17 | if hit and hit.Parent and hit.Parent:FindFirstChild( "Humanoid" ) then |
18 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
19 | local checkpointData = player:WaitForChild( "CheckpointData" ) |
20 | if checkpointData.Value ~ = sp then |
21 | checkpointData.Value = sp |