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

Help with looping script, and debounces?

wait(10)
while true do
	wait(.1)
	local equiped = game.Players.LocalPlayer.Equiped
	local findknife = false
	local debounce = false
	for i,v in pairs(game.Players.LocalPlayer:GetChildren()) do
		if debounce == false then
			if v.ClassName == "StringValue" then
				if v.Value == equiped.Value then
					findknife = true
					debounce = true
				else
					findknife = false
				end
			end
		end
	end
	if findknife == false then
		game.Players.LocalPlayer.Equiped.Value = "Default"
	end
end

I was trying to make a script to prevent players from having something equipped that they don't have in their inventory. However, that didn't work, because it kept setting their weapon back to "default" even if they did have the item in their inventory. My solution to this was to add in the debounce, but now the script does nothing at all. Some help, please?

1 comment

0

Debounce is only useful when you have a 'wait' command (or anything else that takes time to finish) and when the code might start running multiple times before finishing. As for the rest of your script, I recommend you try 'print'ing out values so that you can see for yourself what's going on. (You might need to change the wait(0.1) to wait(5) while doing this so that you don't get a ton of mess

1 answer

in a while true do loop, the script is always firing at the wait interval you set it to. for this loop, you set the script to run every 0.1 seconds. So instead of just activating when an item is equipped, its constantly activated and doesnt know when to stop. To fix this, you need to make a script that activates when the player equips a tool. for the sake of time, lets say you have this script inside the tool. Use this as a referance.



function(onEquip)


-- your code goes in here, no need for a debounce. This only fires when the tool is equipped.


end


script.Parent.Equipped:connect(onEquip)

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