Local Cooldown?

So i made a thing that when you click an NPC, some text appears on screen so he talks to you, and he gives u a tool to complete the task he gives you. But i want to add a cooldown to this so if player clicks the NPC while the NPC is talking it will get bugged, i dont want this to happen. And also i dont want it to give the tool anymore if a player already clicked the NPC and got the tool.

Here is scripts bc its so hard to explain like that:

First this is the script i used in the NPC;

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	game.ReplicatedStorage.guiEventA:FireClient(plr)
	wait(27)
	game.ReplicatedStorage.Bottle:Clone().Parent = plr.Backpack
end)

And then the local script in StarterGui;

local textlabel = script.Parent:WaitForChild("TextLabel")

local player = game.Players.LocalPlayer
local char = player.Character

game.ReplicatedStorage.guiEventA.OnClientEvent:Connect(function(plr)
	local function typewrite(object,text,length)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			wait(length)
		end
	end  
	typewrite(textlabel,"Ah... Seems like someone was able to find this tiny place. So what brings you here, sir?",0.01)
	wait(1)
	typewrite(textlabel,"There's not much to do here... But if you agree, you can help me with something.",0.01)
	wait(1)
	typewrite(textlabel,"About 15 years ago, I had a cerebral palsy during a ritual. Because of this cerebral palsy, I immediately forget what I went through. Since then, I have not been interested in rituals...",0.01)
	wait(1)
	typewrite(textlabel,"But there is a way to get over this disease. For this, I need some water brought from the sacred fountain on this mountain, which was built in ancient times.",0.01)
	wait(1)
	typewrite(textlabel,"If you bring me some of this water, I will try to help you with some stuff. Here's a small bottle you can put the water in.",0.01)
	wait(1)
	typewrite(textlabel,"And please be careful, the way to fountain is dangerous. I'll be praying for you...",0.01)
	wait(2)
	typewrite(textlabel," ",0.01)
end)

I tried to add a debounce to this local script, it worked but i cannot find a way to fix the problem about the tool.
Sorry if it was hard to understand, this is my first topic so it was a bit confusing, but if someone can help thanks a lot!

Thanks again! :smile:

For what I can see, this code Is supposed to run when you click the NPC, then it waits 27 seconds to clone an Item to the player’s backpack.

And this is the code you used to make the NPC speech.

According to your exaplanation, you want to prevent the player from talking to the NPC again and re-getting the item. What I recommend doing Is disabling the ClickDetector once the Item was give by doing this:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	game.ReplicatedStorage.guiEventA:FireClient(plr)
	wait(27)
	game.ReplicatedStorage.Bottle:Clone().Parent = plr.Backpack
    script.Parent.ClickDetector:Destroy()
end)

After the player gets the Item, the ClickDetector is destroyed, preventing from the player from talking to the NPC again. Hope that helped!

Try this code replace the code from your npc script with this code

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if not plr.Backpack:FindFirstChild("Bottle") then
		game.ReplicatedStorage.guiEventA:FireClient(plr)
		game.ReplicatedStorage.Bottle:Clone().Parent = plr.Backpack
	end
end)
1 Like

My code example was quite worse than yours, since the Backpack tries to find the Bottle. If It doesn’t, then It runs the function, for what I could understand.

But this will do it for the server, so any player wont be able to get a task after that. I want to do this only with client

Mine just simply destroys the ClickDetector, which could be a bad idea.

Oh, I get It, then your game would fail, obviously.

what client does is set the max distance of reaching the click detector to 0
and blocks from gettings the tool again
Local script :

local textlabel = script.Parent:WaitForChild("TextLabel")

local player = game.Players.LocalPlayer
local char = player.Character
local Npc = TheInstanceOfTheNpcWhereTheClickDetectorExists
game.ReplicatedStorage.guiEventA.OnClientEvent:Connect(function(plr)
local SavedClickDetectorDistance = Npc.ClickDetector.MaxActivationDistance
Npc.ClickDetector.MaxActivationDistance = 0
	local function typewrite(object,text,length)
		for i = 1,#text,1 do
			object.Text = string.sub(text,1,i)
			wait(length)
		end
	end  
	typewrite(textlabel,"Ah... Seems like someone was able to find this tiny place. So what brings you here, sir?",0.01)
	wait(1)
	typewrite(textlabel,"There's not much to do here... But if you agree, you can help me with something.",0.01)
	wait(1)
	typewrite(textlabel,"About 15 years ago, I had a cerebral palsy during a ritual. Because of this cerebral palsy, I immediately forget what I went through. Since then, I have not been interested in rituals...",0.01)
	wait(1)
	typewrite(textlabel,"But there is a way to get over this disease. For this, I need some water brought from the sacred fountain on this mountain, which was built in ancient times.",0.01)
	wait(1)
	typewrite(textlabel,"If you bring me some of this water, I will try to help you with some stuff. Here's a small bottle you can put the water in.",0.01)
	wait(1)
	typewrite(textlabel,"And please be careful, the way to fountain is dangerous. I'll be praying for you...",0.01)
	wait(2)
	typewrite(textlabel," ",0.01)
-- Npc.ClickDetector.MaxActivationDistance = SavedDistance 
end)

Non local script :

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	game.ReplicatedStorage.guiEventA:FireClient(plr)
	wait(27)
     if not plr.Backpack:FindFirstChild("Bottle") and not plr.Character:FindFirstChild("Bottle") then
	game.ReplicatedStorage.Bottle:Clone().Parent = plr.Backpack
    game.ReplicatedStorage.Bottle:Clone().Parent = plr.StarterGear
end
end)

Wow i really didnt think like that, i think it will work ill try that right now. Thanks

This is so helpfull and what i want. Thanks for help! I’ll try that too

dont forget to change the instance of the npc model in the local script
else it wont work

I think ill try to add a debounce instead of disactivating the clickDetector, but ill use the script u send for the NPC

there is no need since they cant click the clickdetector whoever clicked it anymore
the distance is 0 which cant be clicked unless people exploit but i added a anti tool clone just for that reason.

Seems like true, im trying these scripts rn so when im done with that ill tell the results, thanks!

i have 1 question though
do you want the tool to stay after they respawn /die?
or either
stay even after you rejoin
or nothing above?

I think i want it to stay after they respawn or die, but staying even after you rejoin is not needed

okay i will edit the script so it has that (done)

Thanks ! This will help a lot! [Helped]

Instead of waiting 27 seconds i suggest you remove the last 2 lines of code
and do the following

Fire RemoteEvent at the end To inform the server that the dialog ended

wait(2)
typewrite(textlabel," ",0.01)
game:GetService("ReplicatedStorage").DialogEndedA:FireServer() -- example name

ServerScript:

local t = {}

game:GetService("ReplicatedStorage").DialogEndedA.OnServerEvent:Connect(function(plr)
    if not t[plr.UserId] == game.ReplicatedStorage.Bottle.Name then -- Use the name because clones are seperate instances
       game.ReplicatedStorage.Bottle:Clone().Parent = plr.Backpack
       t[plr.UserId] = game.ReplicatedStorage.Bottle.Name --
    end
end)

This is not complete sorry

This is an option too, thanks!