Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.