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

Why doesnt the remote event wait to execute?

LocalScript

local rep = game.ReplicatedStorage
local mouseDetection = rep.MouseDetection
local UIS = game:GetService("UserInputService")
local delayed = false
local delayTime = 0
local equipped = false
local falseCanHit = rep.CantHit
local tool = script.Parent

tool.Equipped:Connect(function()
	equipped = true
end)
tool.Unequipped:Connect(function()
	equipped = false
end)
UIS.InputBegan:Connect(function(input)
	if delayed == false then
		if equipped == true and input.UserInputType == Enum.UserInputType.MouseButton1 then
			delayed = true
			mouseDetection:FireServer()
			if delayed == true then
				task.wait(delayTime)
				falseCanHit:FireServer()
				delayed = false
			end
		end
	end
end)
while true do
	wait(2)
	print(delayed)
end

ServerScript

local rep = game:GetService("ReplicatedStorage")
local mouseDetection = rep:WaitForChild("MouseDetection")
local canHit = true
local db = false
local notMoving = false
local delayed = false
local delayTime = 1.50
local cantHit = rep:WaitForChild("CantHit")
local tool = script.Parent
mouseDetection.OnServerEvent:Connect(function(player)
	canHit = true
	print(canHit)
end)
cantHit.OnServerEvent:Connect(function(player, delayTime)
	canHit = false
end)

1 comment

1

What event does not / is supposed to wait before executing? falseCanHit will be executed after task.wait(0) which is defaulted to 0.025 seconds, mouseDetection will be executed instantly.

1 answer

✓ Accepted answer

Hello again! It seems you've used my advice, and I appreciate that!...However, you might not understand the proper approach to this:

local rep = game.ReplicatedStorage
local mouseDetection = rep.MouseDetection
local UIS = game:GetService("UserInputService")
local delayed = false
local delayTime = 0
local equipped = false
local falseCanHit = rep.CantHit
local tool = script.Parent
 
tool.Equipped:Connect(function() -- what's this for?
	equipped = true
end)

tool.Unequipped:Connect(function() -- again, what are you doing for this?
	equipped = false
end)
	
UIS.InputBegan:Connect(function(input)
	if delayed == false then 
						-- if not delay*
		if equipped == true and input.UserInputType == Enum.UserInputType.MouseButton1 then 
	-- if equipped and input.UserInputType == Enum.UserInputType.MouseButton1 then*
			delayed = true
		mouseDetection:FireServer()
		if delayed == true then -- there's no need for this.
			task.wait(delayTime) -- 0 seconds = instant execution.
			falseCanHit:FireServer()
			delayed = false
			end
		end
	end
end)

while true do
	wait(2)
	print(delayed) 
end

-- LocalScript

Now, there's a lot of reasons why it won't work. For example: you done if delayed == false then. In reality, you should just do if not delayed then since not = the opposite/not true.

Same for if equipped ==... just do if equipped, because you're checking if it's true. This is a lifesaver if you wanna save time on coding. Also, keep input.UserInputType ==... the same. That doesn't need much changing.

delayTime is literally 0 seconds, so it will run instantly. and for the if delayed == true then line below mouseDetection:FireServer(), that's not needed either... Also if you want advice on how to do this you will need to be more specific with your answer, I am not getting clear signs on what to do. But, what I WILL do is:

local Rep = game:GetService("ReplicatedStorage") -- good practise
local ToolActivated = Rep.ToolActivated -- change "MouseDetection" to this.
-- local UIS = game:GetService("UserInputService") Don't need this lol.

local Delayed = false
local DelayTime = 1.5 -- I'll explain why.
local Activated = false -- Changed "equipped" to "Activated" for this script.
local CantHit = Rep.CantHit -- Made it too complicated, changed to this.
local Tool = script.Parent

-- Tool.Equipped:Connect(function() Also don't think it's needed.
-- Tool.Unequipped:Connect(function() 

-- If you're trying to find a click, then do this:
Tool.Activated:Connect(function()
	if Delayed then return end -- REMEMBER THIS! I told you about this last time :3

	Equipped = true
	ToolActivated:FireServer()

	task.wait(DelayTime)
	CantHit:FireServer()
	Delayed = false
end)

while task.wait(2) do -- Instead of running as soon as you join, it will run 2 seconds after you join/run the game. This is optional, and not needed, but I find it cool to use.

	print(Delayed) 
end

-- LocalScript

if Delayed then return end If you read my last post I made on your question, return end stops the script here if Delayed is true. returning, again, is returning a value. But return end = no value is being returned. So no excess values are being added so don't worry.

THEN we make Equipped true. Then we fire the ToolActivated to server and wait for DelayTime (1.5 seconds, also proud of you for using task.wait :D). Then we fire CantHit to server too. Then end the function there.

local Rep = game:GetService("ReplicatedStorage")
local ToolActivated = rep:WaitForChild("ToolActivated") -- remember, change the name.
local CanHit = true
local db = false -- delayed + db is just the same thing but named differently.
-- local notMoving = false (this isn't needed)
-- local delayed = false (Not needed-)
-- local delayTime = 1.50 (We already have made the 1.5 seconds in the local script, + that won't carry over to here.

local CantHit = rep:WaitForChild("CantHit")
local Tool = script.Parent

ToolActivated.OnServerEvent:Connect(function(player)
	CanHit = true
	print(canHit)
end)

cantHit.OnServerEvent:Connect(function(player, delayTime)
	CanHit = false
end)


-- ServerScript

Most of the variables you made are not needed whatsoever. delayTime, delayed and notMoving are all not needed for this. for delayTime, it doesn't carry over from the LocalScript to THIS script. It's not how it works. And notMoving barely has a use here, so again NOT needed.

Again, I hope this helped you understand what you need to do, and if you're stuck on anything just ask me!

(Edit: I replaced the while task.wait loop to the local, simple mistake ;w;)

8 comments

1

wow! huge answer for a noob upvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvoteupvote

0

B)

1

Very well written!

0

Awh thank you @NotThatFamouss ! To be honest I thought it was pretty amateur ;w;

0

Holy Crap i am so dumb thank you for pointing that out @Opalmine i set Delay time to 2 for the wrong script

0

it's fine, we all make mistakes

0

i'm jealous now xd you have less than week registration on this site and you already got 4 upvotes and i with my near 4k struggle to reach 3 should i just jump out the window lmoa

0

lol

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