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

How do I debounce a player being added as a stringval into a folder? Explained in the body.

If the title didn't make sense, which it most likely didn't, I'll explain it better here.

Background Information: I have a brick that when you touch it, a server script inside it adds a new string value into a folder thats called "GamePlayers". This string value is basically saying, "This player is inside the game." The thing is, when you touch the brick, the specific player can enter more than once (He can have more than 1 string value). Therefore, I need a way to debounce the player to make sure each player can only have 1 string value to their name.

Question: How do I make a debounce to a player's name when hitting a brick. I have the script here that I was working on that didn't work:

local GamePlayers = workspace:FindFirstChild("GamePlayers")
local debounce = false
script.Parent.Touched:Connect(function(hit)
	if debounce then return end
	debounce = true
	if hit.Parent:FindFirstChild("Humanoid") then
		if GamePlayers:FindFirstChild("Player") then -- checks if there is even a player inside the folder.
			if not GamePlayers.Player.Value == hit.Parent.Name then -- ||MY FAILED PLAYER DEBOUNCE|| --
			
				GamePlayers.Player.Value[hit.Parent.Name] = true -- My failed debounce again
				
				local NewPlayer = Instance.new("StringVal")
				NewPlayer.Parent = GamePlayers
				NewPlayer.Name = "Player"--All players values are named Player so the thing can check if there is a stringval with the value of their name.
				NewPlayer.Value = hit.Parent.Name
			
		else
			GamePlayers.Player.Value[hit.Parent.Name] = true -- My failed debounce again
				
			local NewPlayer = Instance.new("StringVal")
			NewPlayer.Parent = GamePlayers
			NewPlayer.Name = "Player"--All players values are named Player so the thing can check if there is a stringval with the value of their name.
			NewPlayer.Value = hit.Parent.Name
		end
	end
	wait(0.5)
	debounce = false
	GamePlayers.Player.Value[hit.Parent.Name] = false -- My failed debounce ONCE AGAIN
	
end)

1 answer

✓ Accepted answer

There are a some small logic errors in your code and I just think you are overcomplicating this. On line 7 GamePlayers:FindFirstChild("Player") you check for a child named Player which is the name of your string value holder. This means you are just looking for a static name and not looking for the one that the player owns?

I am not sure why you were trying to index the value object sting and set it to true GamePlayers.Player.Value[hit.Parent.Name] = true. e.g ("some players name") = true which is not valid you cannot assing a new value to a string.

Lastly you also have a missing end on line 16.

Using a table

I am not sure why you are using value objects it would be far better to just use a table to keep track of the players. It is far more simple to add and remove keys from a table than it is to add and remove a value object.

local lst = {} -- new table
local plrServ = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local plr = plrServ:GetPlayerFromCharacter(hit.Parent)
	if plr then -- check for player
		-- set key player name => true
		lst[plr.Name] = true -- true is just the place holder 
	end
end)

Using an instance

In your code you had the basic idea in place. With some small changes you should be able to get it to work.

  • Name the value object after the players name
  • Using the players name use FindFirstChild to check for a value object.

Example building from your code

local GamePlayers = workspace.GamePlayers -- find first child is not needed we know it exists

script.Parent.Touched:Connect(function(hit)

	-- a better option would be to use GetPlayerFromCharacter Model
	if hit.Parent:FindFirstChild("Humanoid") then
		-- we only need to add a value container if there is not one found XD
		if not GamePlayers:FindFirstChild(hit.Parent.Name) then 
			
			local NewPlayer = Instance.new("StringVal")
			
			NewPlayer.Name = hit.Parent.Name
			NewPlayer.Value = hit.Parent.Name
			NewPlayer.Parent = GamePlayers -- parent should be set last follow create -> setup -> add events -> parent to the game
		end
	end

end)

Notes

I am not sure why you would need a debounce here as it would only prevent other Humanoids/Players.

If you need a debounce then you will need to think should this be per player or per touched? then decide how to set this up ie use a table to store the current debounce players or just a boolean variable.

I hope this helps. Please comment if you have any other questions.

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