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

how would i make a script only run once per player and a counter that saves through servers?

Asked by 2 years ago

im making a game where you find endings with my brother. when you get an ending it kicks you from the game showing what ending you got. i am tasked to make a endings menu that counts how many endings youve gotten. i have started out with testing on one ending

script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("HumanoidRootPart") then     
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        local label = player.PlayerGui.EndingGui.Menu.Easyendings.updatable
        if player then 
            label.Text = tonumber(label.Text) + 1
        end

    end
end)

problem is obviously it doesnt save through server, and it also doesnt only run once per player through servers aswell. what could i use/do to save stuff through server and only allow one per player?

1 answer

Log in to vote
0
Answered by
SuperPuiu 497 Moderation Voter
2 years ago
Edited 2 years ago

You can either make a table and store player's name and then when someone passes it checks for the name, or you can use values that will be named by the player touched the part and then store it under script and check if it finds it. Example 1:

script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("HumanoidRootPart") then     
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        local label = player.PlayerGui.EndingGui.Menu.Easyendings.updatable
        if player then
          if not script:FindFirstChild(player.Name) then
            label.Text = tonumber(label.Text) + 1
            newVal = Instance.new("StringValue")
            newVal.Name = player.Name
            newVal.Parent = script
           end        
           end

    end
end)

Example 2:

local tabl = {}
script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("HumanoidRootPart") then     
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        local label = player.PlayerGui.EndingGui.Menu.Easyendings.updatable
        if player then
          if not table.find(tabl, player.Name) then
            label.Text = tonumber(label.Text) + 1
            table.insert(tabl, player.Name)
           end        
           end
    end
end)

For saving use DataStore service.

Ad

Answer this question