Thank you for reading this. I am trying to make a dialogue script, but whenever i bind this function to the key f, the function keeps running every frame.

LocalScript:

local module = require(workspace.Region3s.Module)
local reg = workspace.Region3s.Talk
local frame = script.Parent

local function ShowDialougeBox(talkTo)
	frame.name.Text = talkTo
	
	frame:TweenPosition(UDim2.new(0.76, 0, 0.76, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.3)
end

local function PrintText(text, option1, option2)
	module.PrintText(frame.Dialouge, text)
end

while wait() do
	if module.CheckReg3(module.CorrectRegion3(reg.I.bleft, reg.I.tright)) then
		game.ContextActionService:BindAction('Initiate conversation', function()
			print('F pressed')
			
			ShowDialougeBox('???')
			PrintText('Testing testing 123')
		end, false, 'f')
	else
		game.ContextActionService:UnbindAction('Initiate conversation')
	end
end

Module script:

local module = {}

module.CorrectRegion3 = function(bleft, tright)
	local correctedRegion = Region3.new(
		Vector3.new(math.min(bleft.Position.X, tright.Position.X), math.min(bleft.Position.Y, tright.Position.Y), math.min(bleft.Position.Z, tright.Position.Z)),
		Vector3.new(math.max(bleft.Position.X, tright.Position.X), math.max(bleft.Position.Y, tright.Position.Y), math.max(bleft.Position.Z, tright.Position.Z))
	)
	
	return correctedRegion
end
	
module.CheckReg3 = function(reg)
	for i, part in pairs(workspace:FindPartsInRegion3(reg)) do
		if game.Players:GetPlayerFromCharacter(part.Parent) then
			return true
		end
	end
	
	return false
end

module.PrintText = function(textLabel, textToPrint)
	textLabel.Text = ''

	for i, letter in pairs(textToPrint:split('')) do
		textLabel.Text = textLabel.Text..letter
		wait(.055)
	end
end

return module

4 comments

0

because of while loop, you are looping and binding the function every frame

0

Yes, that is true, but the function should only run when I press F, right?

0

yeah it should, no idea why in your case it runs "every" frame.

0

I'll try to prevent the function from being binded every frame.