Disabling parts for an amount of time for a certain player

Hey there! I’ve been trying to code a reward chest which gives players every 5 minutes an reward whenever they step on it, the problem I have is that if a player collects the reward everyone else in the server is not able to collect it until debounce is disabled again.

My local script in StarterPlayerScripts:

debounce = false
local Collector = game.Workspace.Collect

function onTouch(hit)
if hit.Parent:FindFirstChild(“Humanoid”) ~= nil and debounce == false then
if game.Players:FindFirstChild(hit.Parent.Name) ~= nil then
local player = game.Players:FindFirstChild(hit.Parent.Name)
script.Sound:Play()
local amount = math.random(250, 400)
local amount2 = 0
game.ReplicatedStorage.Collected:FireServer(amount, amount2)
print(amount)
print(amount2)
Collector.Transparency = 1
Collector.CanCollide = false
debounce = true
wait(300)
Collector.Transparency = 0.8
Collector.CanCollide = false
debounce = false

	end
end

end

game.Workspace.Collect.Touched:Connect(onTouch)

I think the easiest way around this would be to make the ‘collector’ in the local script too. That way every player can only see/interact with their own:

debounce = false
local Collector=Instance.new("Part")
Collector.Parent=workspace
Collector.Name="Collect"
Collector.Anchored=true
Collector.Position=Vector3.new(-6.2, 0.5, -11.52)

function onTouch(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil and debounce == false then
if game.Players:FindFirstChild(hit.Parent.Name) ~= nil then
local player = game.Players:FindFirstChild(hit.Parent.Name)
script.Sound:Play()
local amount = math.random(250, 400)
local amount2 = 0
game.ReplicatedStorage.Collected:FireServer(amount, amount2)
print(amount)
print(amount2)
Collector.Transparency = 1
Collector.CanCollide = false
debounce = true
wait(300)
Collector.Transparency = 0.8
Collector.CanCollide = false
debounce = false

	end
end
end

game.Workspace.Collect.Touched:Connect(onTouch)
3 Likes

If this is in StarterPlayerScripts, then this means that this function will happen for everyone whenever someone touches it with the debounce off. A solution would be to handle the on touch event from the Serverside and use :GetPlayerFromCharacter() to fire a RemoteEvent to the LocalScript of that specific player. Have the LocalScript check if it matches the LocalPlayer, if it does, then disable the chest for it.

edit: ZombieCrunchUK’s method also works fine. It’s a lot simpler to implement too.

3 Likes

Thank you for the fast help It did work perfectly!

1 Like

Thank you for your method, I will give it a try aswell!

1 Like