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

Why does the first event repeat?

game.ReplicatedStorage.Event.TSDamage.OnInvoke = function()
	if game.ReplicatedStorage.Event.InUse.Value == true then
		for i, Humans in pairs (game.Workspace:GetChildren()) do
			if Humans:FindFirstChild("Humanoid") then
			hum = Humans:FindFirstChild("Humanoid")
			oldhealth = hum.Health
			oldmax = hum.MaxHealth
				hum.MaxHealth = math.huge
				hum.Health = math.huge
		end
		end 
		
	end
end

game.ReplicatedStorage.Event.AfterTS.OnInvoke = function()
if game.ReplicatedStorage.Event.InUse.Value == false then
	for i, Humans in pairs (game.Workspace:GetChildren()) do
		if Humans:FindFirstChild("Humanoid") then
			hum = Humans:FindFirstChild("Humanoid")
			hum.MaxHealth = oldmax
			hum.Health = oldhealth
		end
	end 

end


end

what this does is that when an event is fire, it basically remembers all the players' health and then sets their health to infinity. and then once the second health is fired, the players' health is reverted back to normal. whats happening here is that the first event is being repeated loads of times so that the script remembers the players infinite health instead of the original one so when the second event is fired, the players' health is still infinity and not reverted.

F key script

local input = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

input.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.F then
		game.ReplicatedStorage.Event.TimeStopEvent:FireServer()
		if game.ReplicatedStorage.Event.InUse.Value == false then
		for i = 70, 200, 5 do
			wait()
			workspace.Camera.FieldOfView = i
		end
		for i = 200, 70, -5 do
			wait()
			workspace.Camera.FieldOfView = i
		end
		end
	else
		
	end
end)



9 comments

0

This has to do with the event being invoked i think, im not sure though

0

Hi, is this script a local or server script?

0

server script which is why it's invoked

0

Did you change the "InUse" value in a server script? That might be the problem

0

i changed it to yes when the first event is triggered and i changed it to no when the second event is triggered. i also removed the inuse from the code completely but it wasnt the problem as the problem still persisted.

0

Under what condition is the event triggered?

0

When i press the F key which is received by the server sided script. Then the server sided script triggers the event. I postted the server sided script on my other question.

0

Can you post your F key script here?

0

ok i edited the script

2 answers

Alright, actually you don't need to post the F key script, I'll just give you some example codes.

I believe why this is happening is because when the player presses the "F" key, the input is captured multiple times at once. To solve this, you just need to add a debounce.

It will look something like this:

local uis = game:GetService("UserInputService")
local F_KeyPressed = false -- This is called a debounce

uis.InputBegan:Connect(function(input)
	if ((input.KeyCode == Enum.KeyCode.F) and (F_KeyPressed == false)) then
		F_KeyPressed = true
		-- Fire the events
		wait(2) -- You can change to whatever value you like
		F_KeyPressed = false
	end
end)

NOTE: The codes above are not tested, I'm just giving an example.

A debounce ensures that the "F" key can only be pressed after a certain timing.

Also, you are using a remote function, therefore, you will need to return something by using the “return” keyword. Otherwise, the codes will not continue to run until you return one. (Other than the debounce problem, this is one of the problems too)

2 comments

0

it didnt work

0

also how do i use the return keyword?

I found the issue. I invoked the BindableEvent lots of times because it was under the "for" loop. So i just moved it above the loop and it worked.

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