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

Can someone repair my script?It has errors that i do not know how to repair.

Asked by
DashDQ 17
4 years ago
Edited 4 years ago

--My script-- function PlayerEntered(newPlayer) local stats = Instance.new("IntValue") stats.Parent = newPlayer stats.Name = "leaderstats"

local currency = Instance.new("IntValue") currency.Parent = stats currency.Name = "Thiccness" currency.Value = 0 end

game.Players.ChildAdded:connect(PlayerEntered)

local datastore = game:GetService("DataStoreService"):GetDataStore("Thiccness")

game.Players.PlayerRemoving:Connect(function(leaver) datastore:SetAsync("player_"..leaver.userId,Thiccness) end)

local key = "player_"..Thiccness.UserId

local getsave = datastore:getAsync(key) Thiccness.Value = getsave

--Errors-- 17:57:19.769 - ServerScriptService.leaderstats:20: attempt to index global 'Thiccness' (a nil value)

I need help.I was trying to make an leaderstats that worked but it doesn t saved the progress.So i wrote more scripts that i find on youtube or this platform and i got all of this errors that i don t understand how to resolve.

Can someone please help me please?

0
Can you show me what line 20 of the script is? I'm not sure which one to fix Decryptional 15 — 4y
0
local key = "player_"..Thiccness.UserId --that is line 20-- DashDQ 17 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

local datastore = game:GetService("DataStoreService"):GetDataStore("Thiccness")

function PlayerEntered(newPlayer)

local stats = Instance.new("IntValue") stats.Parent = newPlayer stats.Name = "leaderstats"

local currency = Instance.new("IntValue") currency.Parent = stats currency.Name = "Thiccness" currency.Value = 0

local key = "player_" .. newPlayer.UserId local getsave = datastore:getAsync(key) newPlayer.leaderstats.Thiccness.Value = getsave end

game.Players.ChildAdded:connect(PlayerEntered)

game.Players.PlayerRemoving:Connect(function(leaver) datastore:SetAsync("player_"..leaver.userId, leaver.leaderstats.Thiccness.Value) end)

On line 20, local key = "player_"..Thiccness.UserId, there's no such thing as a Thiccness.UserId. Also the game doesn't know what "Thiccness" is as it is not a variable for anything. You need to go to the leaderstats and set the value there, not just Thiccness.

Also the script is also misplaced in some parts since the getAsync should be in the function where the player joins so the Value from the DataStore can be set. I'm new to LUA so if I made any problems (more experienced coders) please tell me. However, I can tell you that the script above works perfectly fine. I've tested it inside a game and it saves my data. Also only works if you test ingame. Datas don't always save when you are testing inside studio for some reason.

Ad
Log in to vote
0
Answered by 4 years ago

Well, your code is quite difficult to read, learn about making good questions here

but the error message explains everything about what's going wrong..

attempt to index global 'Thiccness' (a nil value) means that your trying to get/set a property, or call method on the variable Thickness which is either undeclared, or is nil

so write your code like this:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService");
local Datastore = DataStoreService:GetDataStore("--name of datastore goes here")

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

    local currency = Instance.new("IntValue", stats);
    currency.Name   = "Thickness"

    local key = "player"..player.UserId
    local thickness = Datastore:GetAsync(key) or 0;

    currency.Value = thickness
end)



Players.PlayerRemoving:Connect(function(player)
    local key = "player"..player.UserId
    local currency = player.leaderstats.Thickness

    Dastore:SetAsync(key,currency.Value)
end)

Answer this question