Interaction Script not working

So I’m making my Fire model, and I made some animations that I want to play when a player presses E (Originally it was I, but that was already binded), but this won’t work. My script:

local Player = game.Players.LocalPlayer

game.ReplicatedStorage.GUIs.ShowInteraction.OnClientEvent:Connect(function(purposeOfInteraction, partFired)
	print("received event")
	if purposeOfInteraction == "Fire" then
		if (Player.Character.HumanoidRootPart.Position - partFired.Parent.FlamePart.Position).Magnitude <= 5 then
			script.Parent.Visible = true
		else
			script.Parent.Visible = false
		end
	end
	
	local UIS = game:GetService("UserInputService")
	local IKey = Enum.KeyCode.E
	local function IisDown()
		return UIS:IsKeyDown(IKey)
	end
	local function IisDownCont(input, gameProcessedEvent)
		print("Started IisDownCont function")
		if IisDown() then
			print("I is Down")
			if purposeOfInteraction == "Fire" then
				print("Purpose is Fire")
				local animSit = Player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Sit)
				local animRubHands = Player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.RubHands)
				local animStand = Player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Stand)
				if (Player.Character.HumanoidRootPart.Position - partFired.Parent.FlamePart.Position).Magnitude <= 3 then
					print("Humanoid is in range")
					local function sitAnim()
						if not animSit.IsPlaying then
							print("Sit animation is not playing, starting play.")
							animSit:Play()
							print("played, disconnecting")
							sitAnim:Disconnect()
						end
					end
					while true do
						animRubHands:Play()
						wait(3)
						game.Players.LocalPlayer.Statistics.Warmth.Value = game.Players.LocalPlayer.Statistics.Warmth.Value + 10
					end
					local function standAnim()
						if not animStand.IsPlaying then
							print("Stand animation is not playing, starting play.")
							animStand:Play()
							print("played, disconnecting")
							standAnim:Disconnect()
						end
					end
				else
					script.Parent.Visible = false
				end
			end
		end
	end
	UIS.InputBegan:connect(IisDownCont)
end)

If there’s any confusion please let me know.

1 Like

The only issue I see is the while loop preventing the code under it (but not in its body) from executing. The loop acts like a wall to the code under it. The code will not execute until the loop has terminated. You may want to move the loop to the bottom of the function.

Also, UIS and IKey are never going to change, so why declare the variables each time OnClientEvent gets fired?

One declaration will suffice, they should be declared at the top of the script.

1 Like

Sorry, by loop you mean the

while true do
	animRubHands:Play()
	wait(3)
	game.Players.LocalPlayer.Statistics.Warmth.Value = game.Players.LocalPlayer.Statistics.Warmth.Value + 10
end

right?

1 Like

Yes that loop. Since its infinite it will never break, and thus won’t allow the code under it to execute. This control structure should be the very last statement.

1 Like

OK I’m just going to try that. Thanks! :slight_smile:

1 Like

So for some reason it receives the event but never detects a purpose? I verified the placement of the arguments, and I never get anything in my output that suggests that the purpose is invalid, nor are any of the arguments.

1 Like