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

How to make the value of 'BoolValue' remain the same after player dies?

Asked by
Soban06 410 Moderation Voter
3 years ago

I will explain later, first see this local script:

local Player = game.Players.LocalPlayer
local Open = script.Parent.Open

script.Parent.MouseButton1Click:Connect(function()
    if Open.Value == false then
        script.Parent.Text = "2X Speed: On"
        script.Parent.BackgroundTransparency = 0
        Player.Character.Humanoid.WalkSpeed = 25
        Open.Value = true

        elseif Open.Value == true then
        script.Parent.Text = "2X Speed: Off"
        script.Parent.BackgroundTransparency = .5
        Player.Character.Humanoid.WalkSpeed = 16
        Open.Value = false
    end
end)

Explaination:

Inside a textbutton, I have a boolvalue named 'Open' and a local script (the above one). The default value of the boolvalue is set to false.

Now if the player click on the textbutton, then I am going to change the walkspeed of the player.

Seems simple till now?

Now let's suppose the value of the boolvalue is set to true (because the player clicked the textbutton) and on clicking, it toggles the value between true and false. And if the value is set to true (which means that the player's walkspeed will increase to 25), and then if a player dies, the value will be set to false again. And the player (after dying), will have to select on the textbutton again to increase their speed.

Now in my game, the player's will continuously die, because my game is an obby. And people continuously die from lazers etc. So the players won't like it when they will have to again and again press the textbutton every time they die just to increase their speed. Because they will die so many times.

Back to the question:

I don't really know how to set the value to the same value it was before dying, so players don't need to press the text button every time they die.

I don't need datastores:

Because I just want the value to save when the player dies. Not when they leave the game.

Thanks for any help

0
In the properties of the ScreenGui (or Billboard or Surface) that this TextButton is housed in, disable `ResetOnSpawn` Gey4Jesus69 2705 — 3y
0
But why can't I see the value of of Bool change while playing the game? What I mean is, that if the value get's changed via that local script, why can't I see the value of the Bool in the explorer getting changed. It just remains the same although the local script acts like it is changed. Soban06 410 — 3y
0
I tried you way, yes the gui still says '2X Speed: On' but it resets the walkspeed whenever the player dies. Anyway to fix that? Soban06 410 — 3y

1 answer

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

Answer: Use remote functions and server storage. 1. Put a remote function in Replicated Storage so client and server can access it. 2. Add a folder of the player's x2 speed values. 3. Add a BoolValue of the player's name in the folder when they join using a server script in ServerScriptService.

local speedFolder = Instance.new("Folder")
speedFolder.Name = "DoubledSpeed"
speedFolder.Parent = game.ServerStorage

game.Players.PlayerAdded:Connect(function(player)
    local plrBoolValue = Instance.new("BoolValue")
    plrBoolValue.Name = player.Name
    plrBoolValue.Value = false
    plrBoolValue.Parent = speedFolder
end)
  1. Do a check function for checking the BoolValue in the folder. Needs to be a server script again.
-- IF SEPARATE SERVER SCRIPT FROM ABOVE SCRIPT:
local speedFolder = game.ServerStorage:WaitForChild("SpeedFolder")
local remoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction") -- "RemoteFunction" must be replaced with the name of the remote function in replicated storage

remoteFunction.OnServerInvoke = function(player)
    local doublePlayerSpeed = speedFolder:FindFirstChild(player.Name)
    assert(doublePlayerSpeed ~= nil or doublePlayerSpeed:IsA("BoolValue"), "Player double speed value not found") -- errors if the BoolValue was not found/it isn't a BoolValue.
    if doublePlayerSpeed.Value == true then -- if the speed is doubled
        local character = player.Character or player.CharacterAdded:Wait() -- makes sure "character" variable is not nil
            character.Humanoid.WalkSpeed = 16 -- changes the speed
            doublePlayerSpeed.Value = false -- changes the BoolValue's value
        return true -- returns the condition the BoolValue was found in.
    elseif doublePlayerSpeed.Value == false then -- if the speed is not doubled
        local character = player.Character or player.CharacterAdded:Wait()
        character.Humanoid.Walkspeed = 25
        doublePlayerSpeed.Value = true
        return false
    end
end

-- IF SAME SCRIPT AS EARLIER PLAYERADDED SCRIPT:
local remoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction") -- "RemoteFunction" must be replaced with the name of the remote function in replicated storage

remoteFunction.OnServerInvoke = function(player)
    local doublePlayerSpeed = speedFolder:FindFirstChild(player.Name)
    assert(doublePlayerSpeed ~= nil or doublePlayerSpeed:IsA("BoolValue"), "Player double speed value not found") -- errors if the BoolValue was not found/it isn't a BoolValue.
    if doublePlayerSpeed.Value == true then -- if the speed is doubled
        local character = player.Character or player.CharacterAdded:Wait()
            character.Humanoid.WalkSpeed = 16
            doublePlayerSpeed.Value = false
        return true
    elseif doublePlayerSpeed.Value == false then -- if the speed is not doubled
        local character = player.Character or player.CharacterAdded:Wait()
        character.Humanoid.Walkspeed = 25
        doublePlayerSpeed.Value = true
        return false
    end
end
  1. Invoke from the local script in text button if the text button was clicked (remote functions return a value/values depending on what you return in the server script/local script):
local remoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction") -- "RemoteFunction" must be replaced with the name of the remote function in replicated storage

script.Parent.MouseButton1Click:Connect(function()
    local doubledSpeed = remoteFunction:InvokeServer()

    if doubledSpeed == false then -- checks if the speed is not doubled
        script.Parent.Text = "2X Speed: On"
        script.Parent.BackgroundTransparency = 0
    elseif doubledSpeed == true then -- checks if the speed is doubled
        script.Parent.Text = "2X Speed: Off"
        script.Parent.BackgroundTransparency = .5
    end
end)
  1. Add an auto-speed setter script to set the speed if the player dies (SERVER SCRIPT):
-- IF SEPARATE SERVER SCRIPT FROM PLAYERADDED SCRIPT:
local speedFolder = game.ServerStorage:WaitForChild("SpeedFolder")

game.Players.PlayerAdded:Connect(function(player) -- if the player is added
    player.CharacterAdded:Connect(function(character) -- if the character is added for the first time
        character.Humanoid.Died:Connect(function()
            character = player.CharacterAdded:Wait() or player.Character -- when the player respawns, character isn't nil
            local speedDoubled = speedFolder:FindFirstChild(player.Name)
            assert(speedDoubled ~= nil or speedDoubled:IsA("BoolValue"), "Double speed value not found") -- checks if the value is not nil or it isn't a BoolValue
            if speedDoubled.Value == true then -- checks if the speed is doubled
                character.Humanoid.WalkSpeed = 25 -- sets the speed value
            elseif speedDoubled.Value == false then -- checks if it's not doubled
                character.Humanoid.WalkSpeed = 16 -- you might not actually need the elseif statement
            end
        end)
    end)
end)

EDIT: 7. Remove the values if the player is leaving to prevent duplicates/memory being wasted (SERVER SCRIPT ASWELL):

-- IF SEPARATE SERVER SCRIPT FROM FIRST PLAYERADDED SCRIPT:
local speedFolder = game.ServerStorage:WaitForChild("SpeedFolder")

game.Players.PlayerRemoving:Connect(function(player)
    local plrBoolValue = speedFolder:FindFirstChild(player.Name)
    assert(plrBoolValue ~= nil or plrBoolValue:IsA("BoolValue"), "Double speed value doesn't exist (2)") -- errors if the bool value is nil or it isn't a BoolValue
    plrBoolValue:Destroy()
end)

ALL DESCRIPTIONS ARE IN THE SCRIPT/INSTRUCTIONS. Hope this helps!

Ad

Answer this question