Ad
Log in to vote
0

My updating variable every second script doesn't work, can you help?

Asked by 14 days ago
Edited 14 days ago

Hi there! I'm making a game where the players' points update by one every second. I need the points to start updating only when the player steps on a model, called "Game." Here is my script, which doesn't work. I hvae tried many times to fix this, but all attempts were unsuccessfull. (I would have added those attempts, but I delete them XD) Can you help me?

local Players = game:GetService("Players")
    local UpdatePoints = false
    local function onTouch(part)
        return end
    end
    part.Touched:Connect(onTouch) 
    Players.PlayerAdded:Connect(function(Player)   
            Player.CharacterAdded:Connect(function(Character)
                    UpdatePoints = true

                    local Leaderstats = Instance.new("Folder")
                    Leaderstats.Name = "leaderstats"
                    Leaderstats.Parent = Player

                    local Points = Instance.new("IntValue")
                    Points.Name = "Points"
                    Points.Parent = Leaderstats

                    Player.leaderstats.Points.Value = 0

                    local Humanoid = Character.Humanoid
                Humanoid.Died:Connect(function()
                            UpdatePoints = false
                            Player.leaderstats.Points.Value = 0
                    end)

                    while (UpdatePoints) do
                            wait(1)
                        Player.leaderstats.Points.Value += 1
                            if not (UpdatePoints) then
                                    break
                            end
                        end
                end)
end)

Also, where would I put this script, if you can fix it. Thank you!

0
first of all, format your script!! i cant read this 0hsa 119 — 14d

1 answer

Log in to vote
0
Answered by
NotedAPI 809 Moderation Voter
14 days ago
Edited 14 days ago

Hey Pug,

I saw a few things wrong with your script and I think fixed all the issues you were having. First, I moved the variable inside of the PlayerAdded event, because I think you only wanted to change the player's points depending on their variable bool value. Second, I moved the creation of stats outside of the CharacterAdded event as that will just add more and more leaderstats folders as they die or whenever it's added. Although, I did keep the HumanoidDied event inside of there.

local Players = game:GetService("Players")

--[[
local function onTouch(part)
    return 
end

part.Touched:Connect(onTouch)]]

Players.PlayerAdded:Connect(function(Player)   
    local UpdatePoints = true

    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
    Leaderstats.Parent = Player

    local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Value = 0
    Points.Parent = Leaderstats

    Player.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            UpdatePoints = false
            Points.Value = 0
        end)
    end)

    while wait(1) do
        if (UpdatePoints) then
            Points.Value += 1
        end
    end
end)
0
Where do I put this? pugguy1000 3 — 12d
Ad

Answer this question