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

How to make a debounce even if a player rejoins it doesnt restart?

I'm trying to do this with every way I can and some ways are like never gonna work, it's weird but I hope I can find an answer to this question. I'm trying to make it so that if a player touches the brick, he can never touch it again or the code inside the touched event will never run again even if he rejoins Here's the code I tried and didn't work.

local debounce = true

script.Parent.Touched:Connect(function(plr)
	
	if plr.Parent:FindFirstChild("Humanoid") then
		
		script.Parent.BrickColor = BrickColor.new("Really red")
		debounce = false
		game.Players.PlayerAdded:Connect(function(player)
			
			player.CharacterAdded:Connect(function(player)
				debounce = false
			end)
		end)
	end
end)

I tried my best ok?

Script

A ServerScript inside a part inside the workspace.

Thanks.

2 comments

0

I think you'll need to use DataStore

0

I will try that ty

1 answer

I believe you are trying to make it one-time touch part.

I think you'll need to use DataStore.

Script inside your part:

script.Parent.Touched:Connect(function(hit)
	local playerCharacter = hit.Parent.Name
	local player = game.Players[playerCharacter]
	local partDebounce = player.canTouch
	if partDebounce.Value == false then -- if the value of canTouch boolean is false
		partDebounce.Value = true
		-- do whatever you want
	else
		print("player cant touch this part anymore")
	end
end)

Script inside ServerScriptService:

local DataStore = game:GetService("DataStoreService")
local debounceData = DataStore:GetDataStore("YourDataStoreHere")

game.Players.PlayerAdded:Connect(function(plr)
	local canTouchPart = Instance.new("BoolValue")
	canTouchPart.Name = "canTouch"
	canTouchPart.Parent = plr

	local data = debounceData:GetAsync(plr.UserId)
	if data then
		plr.canTouch.Value = data
	end
end)

local function saveData(plr)
	local data
	data = plr.canTouch.Value

	debounceData:SetAsync(plr.UserId, data)
end


game.Players.PlayerRemoving:Connect(function(plr) -- runs when player leaves
	saveData(plr)
end)

game:BindToClose(function() wait(1) -- runs when game shutdowns
	for _, v in pairs(game.Players:GetPlayers()) do
		if v then
			saveData(v)
		end
	end
end)

I haven't tested if the data works, but you can try and let me know!

7 comments

1

if you want to do it for multiple parts then simply make a data table

0

so i tried ur code and put each script in its place

0

when i join the game it shows an error and when i touch the part it shows an error

0

Workspace is not a valid member of Players - Line 3 - game.Workspace.Part.Script: Error when i join game

0

canTouch is not a valid member of MeshPart - Line 5 - game.Workspace.Part.Script: Error when I touch part

0

Thanks for helping get an idea on how a one time touch part works and hope you can deal with the errors :D

0

Oh! For line5 error server script is that canTouch is not MeshPart children but player's children

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