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

Who can help me with a TextButton script who reset progress please?

Asked by 2 years ago
Edited 2 years ago

Hey! I have a problem with a script in a button: I want to, when the button is clicked, reset the leaderboard stats (put stage to 0) and kill the player to come back to the spawn. Theres is the scripts: The button script (who dont work):

local leaderstats = Instance.new("Folder")
local button = script.Parent
local checkpoints = workspace:WaitForChild("Checkpoints")

game.Players.PlayerAdded:Connect(function(player)
    local stage = Instance.new("IntValue")
    button.MouseButton1Click:Connect(function()
        stage.Value = 0
        stage.Parent = leaderstats

        button.MouseButton1Click:Connect(function(Hit)
            Hit.Parent.Humanoid.Health = 0
        end)
    end)


end)

The leaderboard and stage script:

local checkpoints = workspace:WaitForChild("Checkpoints")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Value = 0
    stage.Parent = leaderstats

    player.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        wait()
        char:MoveTo(checkpoints[stage.Value].Position)

        hum.Touched:Connect(function(hit)
            if hit.Parent == checkpoints then
                if tonumber(hit.Name) == stage.Value + 1 then
                    stage.Value = stage.Value + 1
                end
            end
        end)
    end)
end)

Thanks in advance for your help!

1 answer

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

The button script appears to be invalid and doesn't appear to make sense when assuming it's a separate local script running inside TextButton on the client

You need to utilize RemoteEvents (https://create.roblox.com/docs/scripting/events/remote-events-and-functions) by making TextButton send RemoteEvent signal (from the client) that causes the server to reset stage value of that specific player like this

Local Script

local remoteEvent = game.ReplicatedStorage:WaitForChild("ResetStatEvent") -- You can use any name here

script.Parent.Activated:Connect(function())
    remoteEvent:FireServer()
end)

Server Script

local checkpoints = workspace:WaitForChild("Checkpoints")
local remoteEvent = game.ReplicatedStorage:WaitForChild("ResetStatEvent") -- Again, you can use any name here

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Value = 0
    stage.Parent = leaderstats

    player.CharacterAdded:Connect(function(char)
            local hum = char:WaitForChild("Humanoid")
            wait()
            char:MoveTo(checkpoints[stage.Value].Position)

        hum.Touched:Connect(function(hit)
                    if hit.Parent == checkpoints then
                if tonumber(hit.Name) == stage.Value + 1 then
                    stage.Value = stage.Value + 1
                end
                    end
        end)
    end)
end)

-- Added code here
remoteEvent.OnServerEvent:Connect(function(plr))
    if plr and plr.Character and plr.Character:FindFirstChild("Humanoid") then
        local humanoid = plr.Character.Humanoid
        plr.leaderstats.Stage.Value = 0
        humanoid.Health = 0
    end
end)

If you find the answer to be working or helped you solve the issue then please accept the answer.

0
TY, it work very well. wDaerkfeX 37 — 2y
0
No problem. Somone_exe 224 — 2y
Ad

Answer this question