Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 2 months ago

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.

0
I think you'll need to use DataStore Feelings_La 269 — 2mo
0
I will try that ty ZombieApocalypz3 35 — 2mo

1 answer

Log in to vote
1
Answered by 2 months ago

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!

1
if you want to do it for multiple parts then simply make a data table Feelings_La 269 — 2mo
0
so i tried ur code and put each script in its place ZombieApocalypz3 35 — 2mo
0
when i join the game it shows an error and when i touch the part it shows an error ZombieApocalypz3 35 — 2mo
0
Workspace is not a valid member of Players - Line 3 - game.Workspace.Part.Script: Error when i join game ZombieApocalypz3 35 — 2mo
View all comments (3 more)
0
canTouch is not a valid member of MeshPart - Line 5 - game.Workspace.Part.Script: Error when I touch part ZombieApocalypz3 35 — 2mo
0
Thanks for helping get an idea on how a one time touch part works and hope you can deal with the errors :D ZombieApocalypz3 35 — 2mo
0
Oh! For line5 error server script is that canTouch is not MeshPart children but player's children Feelings_La 269 — 2mo
Ad

Answer this question