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

My script isn't recognized leaderstats?

Asked by
zomspi 541 Moderation Voter
5 years ago
Edited 5 years ago

My data store is working but for some reason this local script isn't? I have multiple of these scripts but with different values changed etc. Most work in studio but none work in game?



local button = script.Parent game.Players.PlayerAdded:Connect(function() local purchases = game.Players.LocalPlayer:WaitForChild("leaderstats1"):WaitForChild("Purchases2") if purchases.Value == 3 then print("MVP") button.Visible = true button.Parent:WaitForChild("MVP1"):WaitForChild("TextLabel").Visible = false else print("Bye") button.Visible = false end end)

2 answers

Log in to vote
0
Answered by 5 years ago

There is a few things to note here.

First, the player added function is mainly used on server scripts to capture the local player and do something when they join. Local scripts already do this as they run for each person as soon as they join the game. Also, you should use this to capture the players before using the player added function:

local Players = game:GetService("Players")

After capturing it, you can use player added like this:

Players.PlayerAdded:Connect(function(player)
    --your code here
end)

The player added function is already given the local player as an argument. So there is no need to write out game.Players.LocalPlayer. Also, you should only need to use WaitForChild on the root parent, not its children.

Players.PlayerAdded:Connect(function(player)
    local purchases = player:WaitForChild("leaderstats").Purchases2
end)

Okay, let's move past the player added function and show you the working local script. This is if you want it to start up immmediately:

Client Script

local button = script.Parent
local players = game:GetService("Players")


local purchases = players.LocalPlayer:WaitForChild("leaderstats").Purchases2
if purchases.Value == 3 then
    print("MVP")
    button.Visible = true
    button.Parent:WaitForChild("MVP1").TextLabel.Visible = false
else
    print("Bye")
    button.Visible = false
end

Server Script

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local purchases2 = Instance.new("IntValue")
    purchases2.Name = "Purchases2"
    purchases2.Parent = leaderstats
end)

Note: I don't really like using UpValues, but I put it up there for simplicity sake. In order for the leaderboard to popup in the game, it must be called "leaderstats" exactly.

0
That didn't work zomspi 541 — 5y
0
Hmm, make sure the local script is in starter gui. Other than that, I don't know what you could've done wrong as it was tested before I posted it. SteelMettle1 394 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

leaderstats is better serversided, using the plr parameter of PlayerAdded. also use all the leaderstats in one script so there aren't multiple copies. I'm not sure i really understood ur question so feel free to ask.

Answer this question