Let's say I make a leaderboard and I want to add an increasing value every ten seconds (I'm using FE and RemoteEvents), how would I be able to do that. I tried adding the 'wait()' but I'd prefer another way.
For example, I have an argument like this: game.ReplicatedStorage.statFunc:FireServer(plr, value) game.ReplicatedStorage.statFunc:FireServer("100") but I want the value to increase every 10 seconds.
Script for creating the leaderstats and value:
local rStorage = game:GetService("ReplicatedStorage") local Lead = rStorage:WaitForChild("leadFunc", rStorage) local plr = game.Players.LocalPlayer local function onLeadFired(plr, value) local leader = Instance.new("IntValue") local stats = Instance.new("IntValue") leader.Name = "leaderstats" leader.Parent = plr wait(0.5) stats.Name = "Stats" stats.Value = value stats.Parent = plr.leaderstats end Lead.OnServerEvent:connect(onLeadFired)
Script for changing stats:
local rStorage = game:GetService("ReplicatedStorage") local Stat = rStorage:WaitForChild("statFunc", rStorage) local plr = game.Players.LocalPlayer local function onStatFired(plr, value) local stats = plr.leaderstats.Stats wait(0.5) stats.Value = value end Stat.OnServerEvent:connect(onStatFired)
LocalScript for both:
local rStorage = game:GetService("ReplicatedStorage") local Lead = rStorage:WaitForChild("leadFunc", rStorage) local Stat = rStorage:WaitForChild("statFunc", rStorage) local plr = game.Players.LocalPlayer Lead:FireServer("100") wait(10) Stat:FireServer("200")
Forgive me. I do not know what a code block or inline code. Thanks!
Use a while loop for example.
while wait(10) do --script to change value end
I'll explain it now(not this because its self explanatory but loops)so a loop is a statement that will keep running until something is false/true.There are 3 types of loops while,for and repeat. While will run until something is false like
while true do blah blah end if false then it will stop.
For runs until the increment has been reached.
for i = 1,50 do--once it has ran 50 times it'll stop game.Players.LocalPlayer.Character.Humanoid:TakeDamage(2) end
repeat is will repeat a code until a certain condition is met. By the way while and repeat loops NEED a wait() somewhere in the code or it'll crash.
repeat local i = Instance.new("Part") i.Parent = game.Workspace wait() until game.Players.LocalPlayer.Character.Humanoid.Health == 0
There's also generic for but that isn't a loop don't get it mixed up with loop for. I'm not sure by i don't wanna use wait ya meant that sorry if ya did.