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

How do I make a part that gives leaderstats for as long as your on it?

Asked by 3 years ago

I am trying to make a part that gives leaderstats for every second you on the part and I am having trouble with it.

I was think something like this:

local dataStoreService = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
    local Stat = Instance.new("IntValue")
    Stat.Name = "leaderstats"

    local Time1 = Instance.new("IntValue")
    Time1.Name = "Survival Time"
    Time1.Value = 0

    Time1.Parent = Stat
    Stat.Parent = Player
    end

    function onTouched(part)
        if part.Parent:findFirstChild("Humanoid")~=nil then           
            local x = 1
            local incCur = 1
            while wait(x) do
                for i,plr in pairs(game.Players:GetChildren()) do
                    local ls = plr:FindFirstChild("leaderstats")
                    if ls then
                        local Time = ls:FindFirstChild(Time1)
                        if Time then
                            Time1.Value += 1 + incCur
                        end
                    end
                end
            end
        end

script.Parent.Touched:connect(onTouched)
end
end)

But when I tested it the script didn't work. I need help. Please!

0
what is incCur? sne_123456 439 — 3y
0
it is a number value to tell the script what to add to the leaderstat value after the initial point is added. OriginalBonnieG2 -3 — 3y
0
At this point,I would recommend using remote events sne_123456 439 — 3y
0
Hello! I have finally fixed the script! There were numerous errors and I hope you will discover what you did wrong while comparing both scripts! sne_123456 439 — 3y
View all comments (5 more)
0
I have edited my post below follow the instructions pls sne_123456 439 — 3y
0
Hi thanks for your comment but I have personally tested the script, are you doing something wrong? sne_123456 439 — 3y
0
Can you add me to team create? sne_123456 439 — 3y
0
I can even send you a video to show that its working sne_123456 439 — 3y
0
No thanks, I am done with this kind of thing and I'd rather move on than stress about it. OriginalBonnieG2 -3 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Ah yes, I have tested your script and finally found the issue. You see you created the leaderboard 'survivaltime' within a function. and you name survivaltime = Time1. However, you use this variable 'Time1' in another function. But functions cannot read from other functions. So i suggest making Time1 a global variable. so instead of making a leaderstats inside a function, you should make it at the start of the script. and then modify it inside the function. make sure the thing that fires the function goes at the end of the script!!

Heres an example:

-- Put this code in serverscript service inside a script!

local dataStoreService = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
    local Stat = Instance.new("IntValue")
    Stat.Name = "leaderstats"

    local Time1 = Instance.new("IntValue")
    Time1.Name = "SurvivalTime"
    Time1.Value = 0

    Time1.Parent = Stat
    Stat.Parent = Player
end)


-- put this part of the code inside the part you are detecting collisions for, inside a script.

local part = script.Parent
x = 1
local debounce = false

local function onPartTouched(otherpart)
    local partParent = otherpart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        print("humanoid found!")
        while wait(1) do
            if not debounce then
                debounce = true
                for i,plr in pairs(game.Players:GetChildren()) do
                   local ls = plr:FindFirstChild("leaderstats")
                   if ls then
                      local Time = ls:FindFirstChild('SurvivalTime')
                      if Time then
                          Time.Value = Time.Value + 1
                            wait(1)
                            debounce = false
                   end
              end
          end
      end
  end
end
end
part.Touched:Connect(onPartTouched)

This code is guaranteed to work! if you look closely you get the idea that I create Stats and Time at the start of the script and not in a function, that way, it is a variable everyone knows inside the script!

So all in all, a function is like private information that noone but that exact function can see. So if you create a variable inside a function you cannot use it anywhere else, so dont make Stats and Time inside a function, make it outside a function in the script.

Hope this wasn't too confusing!

Any questions? Just ask!

PS - i think you also forgot to put a local in the function onTouched!!, also make sure to put a bracket at the end of 'end' to close the function of PlayerAdded!!

This code has been edited as to make sure it works!

0
I have done exactly what you have said to do, but it did not work as intended. I tweaked it a bit like putting the script in serverscriptservice and changing local part from script.parent to workspace["Survival Time"] as that is the name of the part, but that didn't work either. I'm at a loss here. OriginalBonnieG2 -3 — 3y
0
I have output open at all time and it seems that not even the print("humanoid found!") worked. OriginalBonnieG2 -3 — 3y
0
I'm done with this OriginalBonnieG2 -3 — 3y
Ad

Answer this question