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

How to make a stored variable automatically add a value to a local player?

Asked by 4 years ago

Hello. I've recently been working on a data store system, which adds a permanent value to a player who steps on a piece. However, I want the player to get points after a timer is finished, instead of stepping on a piece. I've tried making a local script for increasing the players value, but it doesn't work in a local script because the data doesn't permanently save. And when I try to do it in a normal script it gives every person 1 point instead of just giving it to 1 person, but permanently stores it. What could I do in a normal script to give a local player 1 point if the timer ends?

(Btw this is the script I used for the points of a player to go up if they step on a part)


local ting = 0

function onTouched(hit)

if ting == 0 then --debounce check
ting = 1 --activate debounce
check = hit.Parent:FindFirstChild("Humanoid") 

if check ~= nil then

    local user = game.Players:GetPlayerFromCharacter(hit.Parent)
    local stats = user:findFirstChild("leaderstats")

    if stats ~= nil then --If moneyholder exists then
        local cash = stats:findFirstChild("Money")
        cash.Value  = cash.Value +1000
        wait(100)
    end

end

ting = 0
end

end

script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago
Edited 4 years ago

To make a timer, make a module script called TimerSettings. Inside there, add this code:

local TimerSettings = {}

TimerSettings.IsRunning = false
TimerSettings.TimeLeft = 0

return TimerSettings

Now, in order to make the timer run, put this in a regular script:

local TimerSettings = require(whereverYourModuleScriptIs)

while true do
    if TimerSettings.TimeLeft == 0 then --If no more time is left then
        TimerSettings.IsRunning = false --Set that it is not running
        wait(5) --Wait a bit
        TimerSettings.IsRunning = true --Make it running
        TimerSettings.TimeLeft = 30 --Set this to whatever you like
    end

    TimerSettings.TimeLeft = TimerSettings.TimeLeft - 1
    wait(1)
end

Now with that to the side, here is the code to add when the timer is done. Put this in StarterPlayerScripts in a regular script:

local TimerSettings = require(whereverYourModuleScriptIs)

TimerSettings.IsRunning.Changed:Connect(function()
    if not TimerSettings.IsRunning then
        local money = script.Parent.leaderstats.Money
        money.Value = money.Value + 1000
    end
end

Please not that the path that I put to the money value might be incorrect, just tweak it if it is. Hope this helped! :)

Ad

Answer this question