How could I print a players name just once?

Hey! So I’m making a “Winner” part which basically prints a players name if they touch the part. I don’t want to take any credit or anything but the script was made by MarkiPr0឵8 who helped me fix a previous issue which the script had.

Anyways, here is the script:

local Players = game:GetService("Players")
local WinnerCoin = game.ReplicatedStorage:WaitForChild("WinnerCoin")
local Names = {}
local Part = script.Parent

Part.Touched:Connect(function(Hit)

		if not Hit.Parent:FindFirstChildWhichIsA("Humanoid") then return end
		Names[#Names+1] = Hit.Parent.Name
		WinnerCoin.Value = ("Winners: "..(table.concat(Names, ", ")))

end)

What I want to achieve is that when a player touches it, I just want it to print the players name just once, I tried debounce but that isn’t really efficient. Also have heard to store the usernames in a string boolean dictionary but I’m not that familiar with that. :sweat_smile:

Any help is appreciated!

print(Hit.Parent.name)

thats about all. just add that inside the function after the check for the humanoid.

Make a table that stores all the winners, if someone touches the part and their name isn’t on the winner list then add it and print the name

Check if the player.Name is already in the Names table. If it is, then don’t print it again.

How would I check if the player is in the table or not?

you can use table.find() and use a if statment to see if its in there

local Players = game:GetService("Players")
local WinnerCoin = game.ReplicatedStorage:WaitForChild("WinnerCoin")
local Names = {}
local Part = script.Parent
local foundPlayer = false -- boolean which tells if we found the same player

Part.Touched:Connect(function(Hit)

		if not Hit.Parent:FindFirstChildWhichIsA("Humanoid") then return end
		for i,v in ipairs(Names) do
           if v == Hit.Parent.Name then foundPlayer = true break end
         
        end
        if not foundPlayer then
           Names[#Names+1] = Hit.Parent.Name
           WinnerCoin.Value = ("Winners: "..(table.concat(Names, ", ")))
        end
        foundPlayer = false
end)

This is just one way of doing it.

1 Like

Oh that has worked! Thank you! :slight_smile:

1 Like

No problem!

30 chursssssssssssssssssss