So I am having a little bit of a issue with a Simple server player time script, the issue being that it does not wan’t to display the actual text on the surface gui’s text.
Script:
game.Players.PlayerAdded:Connect(function(Player)
local template = game.ReplicatedStorage.TimeTemplate
template:FindFirstChild("Playername").Text = Player.Name
template:FindFirstChild("TimePlayed").Text = Player.leaderstats.Time_Played.Value
template:Clone().Parent = workspace.ServerTimePart.DonationGui.Main
while wait(1) do
Player.leaderstats.Time_Played.Value = Player.leaderstats.Time_Played.Value + 1
end
end)
Error: 10:33:08.097 leaderstats is not a valid member of Player "Players.DaffyDavinko" - Server - ServerTime:3
The leaderstats we’re initially made in another script, which handled data saving too, and it was basically a simple leaderstat script but just with the loading functions and stuff.
-- DonationBoardManager.lua
-- DaffyDavinko
-- 24 April, 2021
-- // Services
local Players = game:GetService("Players")
local ReplicatedStorage = game.ReplicatedStorage
local DataService = game:GetService("DataStoreService")
-- // Variables
local LocalData = DataService:GetDataStore("DonationData")
-- // Functions
local function LoadData(Player)
local DonatedValue = Instance.new("NumberValue")
local TimePlayed = Instance.new("NumberValue")
local leaderstats = Instance.new("Folder")
local Template = game.ReplicatedStorage.DonationTemplate.Template:Clone()
local TimePlayedTemp = game.ReplicatedStorage.TimeTemplate:Clone()
Template.Parent = workspace.DonationBoardPart.DonationGui:WaitForChild("Main")
TimePlayedTemp = workspace.ServerTimePart.DonationGui:WaitForChild("Main")
leaderstats.Parent = Player
DonatedValue.Parent = leaderstats
TimePlayed.Parent = leaderstats
leaderstats.Name = "leaderstats"
DonatedValue.Name = "Donated_Amount"
TimePlayed.Name = "Time_Played"
local PlayerID = Player.UserId
local Data
local Success, Error = pcall(function()
Data = LocalData:GetAsync(PlayerID)
end)
if Success and Data ~= nil then
DonatedValue.Value = Data.DonatedValue
Template.Playername.Text = Player.Name
Template.DonationAmount.Text = Data.DonatedValue
else
warn("DEBUG: Player_Data is nil!")
end
end
local function SaveData(Player)
local PlayerID = Player.UserId
local Data = {
DonatedValue = Player.leaderstats.Donated_Amount.Value,
TimePlayed = Player.leaderstats.Time_Played.Value,
}
local Success, Error = pcall(function()
LocalData:SetAsync(PlayerID, Data)
end)
if Success then
print("DEBUG: Saved data to Player")
else
warn("DEBUG: An error has occurred while saving the data")
end
end
-- // Events
Players.PlayerAdded:Connect(LoadData)
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
SaveData(Player)
end
end)
Place print() calls between lines to see where your script stops. It’s also easier to control your work by using variables to define a location once to avoid error.
local leaderStats = player:WaitForChild("leaderstats")
local timePlayed = leaderStats:WaitForChild("Time_Played")
...
It’ll be easier to read and understand later on when you want to read it. This will be confusing for you to look back at in the future the way it is.
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Player:WaitForChild("leaderstats")
local timePlayed = leaderstats:WaitForChild("Time_Played")
local template = game.ReplicatedStorage.TimeTemplate
template:FindFirstChild("Playername").Text = Player.Name
template:FindFirstChild("TimePlayed").Text = tostring(timePlayed.Value)
template:Clone().Parent = workspace.ServerTimePart.DonationGui.Main
while wait(1) do
if Player then -- so it breaks when a player leaves
timePlayed.Value += 1
else break end
end
end)
Did you get an error in output if you pasted my code directly? I made a variable naming mistake. I edited that word. Also please use print() s to find where this issue lies.
An example is:
print "checkpoint 1"
local Part = Instance.new("Part")
print "checkpoint 2"
Part.Name = "Hello"
print "checkpoint 3"
Part.Size = UDim2.new(0.5,0,0.5,0) -- this should error it expects a vector
print "checkpoint 4"
Part.Position = Vector3.new(0.5,0.5,0.5)
print "checkpoint end"