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

how to fix system message on part touch getting multiplied?

i have an end part that when touched the system will put a message in chat saying the player finished the game, but when it chat it chats like 50 times, please help ty

part touch script

	script.Parent.Touched:Connect(function(part)
		for i,v in pairs(game.Players:GetPlayers()) do
				if part:IsDescendantOf(v.Character) then
			game.ReplicatedStorage.ReachedEnd:FireAllClients(v.Name)
		end
	end
end)

message creator local script

game.ReplicatedStorage.ReachedEnd.OnClientEvent:Connect(function(plrName)

	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[Game] " .. plrName .. " Reached the end",
		Color = Color3.fromRGB(255, 255, 255),
		Font = Enum.Font.Cartoon,
		TextSize = 18,
	})
end)

2 answers

✓ Accepted answer

You can try adding a table for the players who are finished and so if you touch the part, you get added but if touch the part again but already added to the table, it won't send way too many messages.

local finishedplayers = {}
script.Parent.Touched:Connect(function(part)
	for i,v in pairs(game.Players:GetPlayers()) do
		if part:IsDescendantOf(v.Character) and not table.find(finishedplayers,v) then
			table.insert(finishedplayers,1,v)							 
			game.ReplicatedStorage.ReachedEnd:FireAllClients(v.Name) 
		end
	end
end)

4 comments

0

your right, also table.insert does not take the index anymore in the second parameter, so you only need the table and the value

0

evaera also should learn from you because she don't know how to make debounce on the comments

0

noob yapelize this guy is pro, table.find does linear search, player that touched the part is very likely to be the first one to touch it again 5 times in a row cuz every limb, this guy inserts the player at start of table so table.find will find the player immediately

0

what no i though table.insert second parameter died

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