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

How would I make this for only the person who touched it?

Asked by 4 years ago
Edited 4 years ago
-- You can't really pause a function. It will continue to run until the code inside of it is done. However you can just remove the function itself, run it in a loop, and detect if a variable is true or not before running the code.
local Player = game.Players.LocalPlayer
local Label = Player.PlayerGui:WaitForChild("ScreenGui").TextLabel
local InGame = 0
local activated = false
local RATE_PER_SECOND = 1

game:GetService("RunService").Stepped:Connect(function(time, step)
    local increment = RATE_PER_SECOND * step
    if activated == true then
        InGame = InGame + increment 
        if InGame < 10 then
            Label.Text = "0:0"..tostring(InGame)
        elseif InGame < 60 then
            Label.Text = "0:"..tostring(InGame)
        elseif InGame >= 60 then   
            local minutes = math.floor(InGame/60)
            local sec = InGame-minutes*60
            if sec < 10  then
                Label.Text = tostring(minutes)..":0"..tostring(sec)
            else
                Label.Text = tostring(minutes)..":"..tostring(sec)
            end
        end
    end
end)


game.Workspace.Activator.Touched:Connect(function()
    activated = true
end)

game.Workspace.Deactivator.Touched:Connect(function()
    activated = false
end)

This is a local script.

2 answers

Log in to vote
0
Answered by 4 years ago

Alright I got it fixed. Read the --'s because one section goes into a server script, then the rest goes inside a local script.

--Inside a server script located anywhere
game.Players.PlayerAdded:Connect(function(player)
    local activated = Instance.new("BoolValue")
    activated.Name = "Activated"
    activated.Parent = player
end)


game.Workspace.Activator.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
        game.Players[humanoid.Parent.Name].Activated.Value = true
    end
end)

game.Workspace.Deactivator.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
        game.Players[humanoid.Parent.Name].Activated.Value = false
    end
end)


-- Inside a local script
local Player = game.Players.LocalPlayer
local SG = Player.PlayerGui:WaitForChild("ScreenGui")
local Label = SG:WaitForChild("TextLabel")
local InGame = 0
local activated = Player:WaitForChild("Activated")
local RATE_PER_SECOND = 1

game:GetService("RunService").RenderStepped:Connect(function(step)
    local increment = RATE_PER_SECOND * step
    if activated.Value == true then
        InGame = InGame + increment 
        if InGame < 10 then
            Label.Text = "0:0"..tostring(InGame)
        elseif InGame < 60 then
            Label.Text = "0:"..tostring(InGame)
        elseif InGame >= 60 then   
            local minutes = math.floor(InGame/60)
            local sec = InGame-minutes*60
            if sec < 10  then
                Label.Text = tostring(minutes)..":0"..tostring(sec)
            else
                Label.Text = tostring(minutes)..":"..tostring(sec)
            end
        end
    end
end)

Ad
Log in to vote
0
Answered by 4 years ago

Simply try using a local script.

Answer this question