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

I made this script that it should level up, but it doesn't seem to?

Asked by
WN0820 11
4 years ago

When I check the error it says ServerScriptService.LevelUp:2: attempt to index nil with 'FindFirstChild' If anyone can help me I would be gratefull here's the script

local plr = game.Players.LocalPlayer
local Stats = game.Players.LocalPlayer:FindFirstChild("Stats") --This is finds the Stats
local XP = Stats:FindFirstChild("XP") -- This should find XP
if XP.Value > 70 *plr.leaderstats.Level.Value then -- I wanted to do if the XP is higher then 70 times level so what I went times by level is when you get lvl 2 it times it to 140
    plr.leaderstats.Level.Value = plr.leaderstats.Level.Value +1 -- this gives it
end

Hope I explained it right to you guys Anyone can help thanks again

2 answers

Log in to vote
0
Answered by
CjayPlyz 643 Moderation Voter
4 years ago
Edited 4 years ago
local players = game:GetService("Players")

local function onplayeradded (player)
    local leaderstats = Instance.new("Folder")
    local stats = Instance.new("Folder")
    local level = Instance.new("IntValue")
    local maxxp = Instance.new("IntValue")
    local xp = Instance.new("IntValue")

    leaderstats.Name = "leaderstats"
    stats.Name = "stats"
    level.Name = "Level"
    maxxp.Name = "maxxp"
    xp.Name = "xp"

    leaderstats.Parent = player
    stats.Parent = player
    level.Parent = leaderstats
    maxxp.Parent = stats
    xp.Parent = stats

    level.Value = 1
    maxxp.Value = 70
    xp.Value = 0

    while true do
        wait(0.1) --You can change this however you want.
        if xp.Value >= maxxp.Value then
            maxxp.Value = maxxp.Value * 2
            xp.Value = 0
            level.Value = level.Value + 1
        end
    end
end

players.PlayerAdded:Connect(onplayeradded)
0
Could I have a whole working script? WN0820 11 — 4y
0
okay please wait about 10 mins CjayPlyz 643 — 4y
0
hover your mouse on the code and click the "view source" on the upper right. you can copy the code there faster. CjayPlyz 643 — 4y
0
its better to put the while loop in the end of the function.. anything after the while loop inside the function will not run unless the while loop is stopped CjayPlyz 643 — 4y
0
I made a new one, but I will still accept it WN0820 11 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I think your problem is with line 3 it's trying to find the child "XP" when it does not exist as you did not create it with Instance and same for the stats

I would remove the stats stuff you made manually and create it with Instance by checking when a player joins the game with a server script in ServerScriptService

Example:

game.Players.PlayerAdded:Connect(function(plr) -- You can change plr to anything as its just a      local value
local stats = Instance.new("Folder") -- The object you make with instance can be changed
stats.Name = "leaderstats" -- If the name is not leaderstats it will not show on the leaderboard but thats your choice
stats.Parent = plr

local lv = Instance.new("IntValue") 
lv.Name = "Level" -- Name string can be change
lv.Parent = stats

local maxxp = Instance.new("IntValue") 
maxxp.Name = "Max XP" -- Name string can be change
maxxp.Parent = plr

local xp = Instance.new("IntValue") 
xp.Name = "XP" -- Name string can be change
xp.Parent = stats

while wait() do -- Same thing as while true do but It won't crash your game
if xp.Value >= maxxp.Value then
xp.Value = xp.Value - maxxp.Value -- I fixed up your exp decrease script for ya so it does not default it to 0 instead it removes as much xp is needed to level up!
maxxp.Value = maxxp.Value * 2
lv.Value = lv.Value + 1
end
end
end)

If you want this stuff to be saved I would look at datastore tutorials they are complicated for beginners but still is needed for a rpg game!

1
Thanks for the help, but I made a new one I feel bad for you wasting time making this script :/ WN0820 11 — 4y
0
@WN0820 Its fine bro just wanted to help a brother out! RecycleBicycle 60 — 4y
0
Thanks now I don't have to think all day and night that I did something bad and wasted your time! WN0820 11 — 4y

Answer this question