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

Trying to make timer, but doesn't update text, and no errors in the output, how can I fix this?

Asked by 1 year ago

I'm trying to make a timer, when a player buys something, but it doesn't work and it gives no errors.

There are two scripts one local, other is server.

Here is my local script (where a remote function gets fired):

-- Varabiles
local plr = game.Players.LocalPlayer

local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteFunction = replicatedStorage:WaitForChild('PotionRemotes'):WaitForChild('DoubleClicks'):WaitForChild('TimerRemote')


game.Players.PlayerAdded:Connect(function(plr)
    -- keeps firing the remote function.
    while plr.Name do
        wait(1)
        remoteFunction:InvokeServer(plr)
    end
end)

Here is the server script:

-- replicated storage varaibes
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteFunction = replicatedStorage:WaitForChild('PotionRemotes'):WaitForChild('DoubleClicks'):WaitForChild('TimerRemote')



remoteFunction.OnServerInvoke = function(plr)
    -- time varabiles
    local timeLeft = game.ServerStorage.DataFolder:WaitForChild(plr.Name):WaitForChild('PotionsFolder'):WaitForChild('DoubleClicksTimeLeft')
    local isActive = game.ServerStorage.DataFolder:WaitForChild(plr.Name).PotionsFolder:WaitForChild("DoubleClicksActive").Value
    local timeLabel = plr.PlayerGui.DisplayGUI.DoubleClicksTimerFrame.TimeLabel
    local timerFrame = plr.PlayerGui.DisplayGUI.DoubleClicksTimerFrame
    -- checks if a bool value is true
    if isActive.Value == true then
        -- sets some guis active, and visible
        timerFrame.Active = true
        timeLabel.Active = true
        timerFrame.Visible = true

        -- checks if there is anymore time left
        if timeLeft.Value >= 1 then
            -- decreases
            timeLeft.Value = timeLeft.Value - 1
            -- updates text
            timeLabel.Text = timeLeft.Value..' sec left'
        else
            -- if not it'll set is active to false, and the frame invisible.
            isActive.Value = false
            timerFrame.Visible = false
        end
    end
end

Also the timer decreasing and updating text has to be in a server script so I can access the int values, and bool valaues.

0
Why are you using a RemoteFunction when you don't return any value(s)? appxritixn 2235 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

All UI changes (with the exception of BillboardGuis and SurfaceGuis) must be made on the client. After deciding what to put on the UI you should send a remote event from the server to the client to update that UI. If you want the change to be made for all player's UI, just use :FireAllClients().

Ad
Log in to vote
-1
Answered by 1 year ago

I don't think changing gui on a server script would be very good. I think you can use FireAllClients() instead.

0
I need an example of the FireClient way, since I'm not too sure how to do it. starranger4 4 — 1y

Answer this question