Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Updated found leaderboard script to R15 and now broken, please help me understand why?

Asked by 5 years ago
Edited 5 years ago

Hi, me again... every time one of you wonderful people help me I get a little bit closer to not being useless.

My next issue is with a leaderboard script I found that worked but always gave me the following output: Torso is not a valid member of Model

Here is the script (updated to replace Torso with HumanoidRootPart as per my reading on the subject of Torso not being a recognised part of the model):

function onArrival(object)
local player = game.Players:playerFromCharacter(object)
    if player ~= nil then
        local leaderstats = player.leaderstats
        local setlevel = game.Workspace:FindFirstChild(leaderstats.Stage.Value)
        print("gah")
        object.HumanoidRootPart.CFrame = object.HumanoidRootPart.CFrame + Vector3.new(0,3,0)
        wait()
        object.HumanoidRootPart.CFrame = setlevel.CFrame + Vector3.new(0,3,0)
    end
end

function setupBoard(object)
    if object.className == "Player" then
        local addls = Instance.new("IntValue")
        addls.Name = "leaderstats"
        local addstage = Instance.new("IntValue")
        addstage.Name = "Stage"
        addstage.Value = 0
        addstage.Parent = addls
        addls.Parent = object
    end
end

game.Players.PlayerAdded:Connect(setupBoard)
game.Workspace.PlayerAdded:Connect(onArrival)

However, now I get this error: Workspace.Leaderboard:9: attempt to index local 'sl' (a nil value)

If HumanoidRootPart is a valid part of my character I'm not sure why it is breaking the sl variable... any guidance would be great.

Thanks

[edit] During the course of this I found that the above code was outdated and basically broken for my needs... the following is the current working model I'm using:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local stage = Instance.new("IntValue", folder)
 stage.Name = "Stage"

 stage.Value = ds1:GetAsync(plr.UserId) or 1
 ds1:SetAsync(plr.UserId, stage.Value)

 stage.Changed:connect(function()
  ds1:SetAsync(plr.UserId, stage.Value)
 end)
end)
0
Please properly indent your code, use proper events (for instance PlayerAdded as opposed to ChildAdded), use function/variable names that make sense, and use non-deprected methods (for instance, :Connect instead of :connect). User#25115 0 — 5y
0
Apologies, I'll try to do that. As stated I'm still very new to this and this is code that I've 'inherited' rather than written. Those few pointers alone may help explain why updating the code for R15 has not worked, I still need help overall but will look to address the pointers you have listed in the meantime GrumpyTheDaddy 17 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Not sure if it'll help but you can't do

game.Workspace.PlayerAdded

Players aren't added to the workspace, do ChildAdded then check for the child's name in Players.

0
Thanks, that cleared that issue, and makes sense now that I think about it lol... back to the original error now but I think it makes sense to scrap that leaderboard script and find a more up to date one I reckon... with my new knowledge I should be able to find one that isn't so difficult to shoehorn into my game GrumpyTheDaddy 17 — 5y
0
Can't seem to post another reply so will edit my original post with an updated... and working... checkpoint/leaderboard solution. Thanks to those that helped me realise I was using outdated and convoluted code GrumpyTheDaddy 17 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Thanks to the guidance of Phlegethon5778, I've done my best to tidy the code up a bit and updated some of the outdated methods etc (I've edited the OP to include the new code... still won't be perfect, as stated, I'm new to this!). The leaderboard still works but now the only error I get is:

PlayerAdded is not a valid member of Workspace

from line 26... could it be firing before the Player has been added to the Workspace?

Answer this question