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

First Post - Hello! - Strange Issues with a Timer (Stopwatch) Script?

Asked by 4 years ago

Hello folks! So I'm probably 2-3x older than the average developer here (pushing 50) and have zero scripting or programming experience. By even asking this question I’ll expose how much I have to learn:

My game is an obby/puzzle style that times your run to rescue the main character.

I'm currently working through a very odd issue with the timer. If the game is being played solo, it works perfectly: You cross a starting line (touching a brick called "TimerStarter"), and the timer begins counting up 1 second at a time; however, if two people are in the game when a player crosses the start line, the timer starts for that player but increases at 2x the rate it should. If three people are in the game, it increases at 3x the rate. The 2nd or 3rd players are unable to start their timers if this ‘timing multiplier’ has occurred. If the first person leaves, or if only one person is in the game and starts their timer before a 2nd joins, the timer works just fine

The timer functions by referencing a value called “TimerRunning” that is in the player (Set up similar to a leaderstat, but not in the leaderstats folder).

Here is my timer script: (again, please have mercy... )

local Players = game:GetService("Players")
local TouchPart = game.Workspace.TimerStarter
local TimerEndPart = game.Workspace.TimerEnder
local debounce = false

TouchPart.Touched:Connect(function(touched)
    if touched.Name == "UpperTorso" then
        local Player = Players:GetPlayerFromCharacter(touched.Parent)
            if Player then
            Player.leaderstats.Timer.Value = 0
            Player.TimerRunning.Value = true
            if not debounce then
                debounce = true
                    while Player.TimerRunning.Value == true do
                    wait(1)
                    Player.leaderstats.Timer.Value = Player.leaderstats.Timer.Value +1
                    end
                debounce = false
            end
        end
    end
end)

And this snippet of my leaderstats and datastore script adds the value "TimerRunning." to the players. (I left out other unrelated

game.Players.PlayerAdded:Connect(function(player)   
    local TimerRunning = Instance.new("BoolValue")
    TimerRunning.Name = "TimerRunning"
    TimerRunning.Value = false
    TimerRunning.Parent = player
end)

Sorry for the long first post. I appreciate any guidance.

0
If this script for the part is in a script try putting it in a local script, as the timer should only need to be ran on the player. and then when they finish you can just get the server to fetch the time I_Nev 200 — 4y
0
Hmm - I will try that; thank you so much for the quick reply. Tricky_Itch 10 — 4y
1
Very odd thing - I put the same script as posted above into a local script (StarterPlayer.StarterCharacterScripts) and it worked fine when testing. Once published, it works fine with one player, and no longer showed the 'multiplier' issue when two players joined. Even with two players joined the timer started fine for the first player, but then would not start at all for a 2nd player. Tricky_Itch 10 — 4y
0
Add me back on roblox if possible, i'll see if i can help you I_Nev 200 — 4y
View all comments (3 more)
0
Maybe create timer for each player, and let server update its value. From what I see, each player has Timer Script which updates leaderstats. The more player, the more ticks per second it does User#17685 0 — 4y
0
Each player updates or adds a second into the timer User#17685 0 — 4y
0
Thank you roblor12 and I_Nev - I think you're both getting me closer to the issue, similar to 'GeneratedScript' below. Tricky_Itch 10 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

First, I would like to say welcome! I hope you have fun in our little cozy community of developers.

Second, lets talk a bit about why your script (probably) isn't working. At first, I had a bit of trouble finding out what's wrong with it, but after reading the comments, I came to a conclusion. You most likely have the server script in StarterCharacterScripts. The issue is with this, not with the script. You need to place the script either in ServerScriptService or as a child of the part. I'll explain why: StarterCharacterScripts creates a clone of each script inside, and sets it's parent to the character of any players that join. This creates duplicate scripts for each new player (meaning you're handling the .Touched on every player, even for other players, which gives the multiplication effect). You're basically doing something everytime the part is touched, but you're doing it on a new script everytime someone joins. With that being said, make sure to make it a ServerScript (and not a LocalScript) when putting it in ServerScriptService or as a child of the part.

You also use debounce, which is server wide in this case, so I would use a dictionary.

local dict = {};

Then, check if the character is already in the dictionary:

if(dict[h.Parent]) then
-- first time they have touched it, start timer here
     dict[h.Parent] = true;
end

Implemented with your script:

local Players = game:GetService("Players")
local TouchPart = game.Workspace.TimerStarter
local TimerEndPart = game.Workspace.TimerEnder
local dict = {};

TouchPart.Touched:Connect(function(touched)
    if touched.Name == "UpperTorso" then
        local Player = Players:GetPlayerFromCharacter(touched.Parent)
            if Player then
            Player.leaderstats.Timer.Value = 0
            Player.TimerRunning.Value = true
            if not dict[Player] then
                dict[Player] = true
                    while Player.TimerRunning.Value == true do
                    wait(1)
                    Player.leaderstats.Timer.Value = Player.leaderstats.Timer.Value +1
                    end
                dict[Player] = false
            end
        end
    end
end)
0
@GeneratedScript - I can't thank you enough for the detailed explanation & warm welcome. Very clear, and as you guessed; yes, I have the script in StarterCharacterScripts. I'm sure the multiplication effect I'm seeing is happening somehow due to this duplication of the script. I'm off to work, but will experiment with moving the script this evening and report back. Tricky_Itch 10 — 4y
0
OK - I lied and decided to try a bit more before running out this morning - When the script is back in the Workspace I initially had no multiplier issue, but a 2nd player's timer would never start. I think it was related to how my debounce was set up: As if once the while loop is running for one player, the debounce is never reset, so another's can never start. I addressed that and its seems ok. Tricky_Itch 10 — 4y
0
You are correct, in the belief that the debounce interfered with this. There is a better way to handle it overall, which I have edited my script to include. Please refer to that! GeneratedScript 740 — 4y
0
A combination of your advice to move the script to the Workspace, and adjusting my debounce seems to have 100% corrected the issue. I can not begin to tell you how thrilled I am. I have struggled with this issues for a couple of weeks. Its incredible to join a community like this and receive such great, detailed coaching and advice. VERY much appreciated. Tricky_Itch 10 — 4y
Ad

Answer this question