Issue with spinning part using ProximityPrompt

Hi,
I have a question regarding proximity prompt. I’m trying to have it where a player holds “e”, the part will start spinning around. For some reason, it doesn’t seem to work… I was wondering if I could get any help here. I have some idea of what may be the issue.

When I press “E” it doesn’t move at all, and I have little to no clue of what’s going on.

local partrotate = script.Parent


local stopspin = {}
while true do
	partrotate.CFrame = partrotate.CFrame * CFrame.fromEulerAnglesXYZ(0,0,0)-- Change 6 to change speed
	wait(0.01)
end

local startspin = {}
while true do
	partrotate.CFrame = partrotate.CFrame * CFrame.fromEulerAnglesXYZ(0.6,0,0)-- Change 6 to change speed
	wait(0.01)
end

prompt.Triggered:Connect(function()
	if prompt.ActionText == "Start" then
		startspin:Play()
		prompt.ActionText = "Stop"
	else
		stopspin:Play()
		prompt.ActionText = "Start"	
	end
end)

I’m somewhat new to Lua :sweat_smile:

1 Like
  1. stopspin and startspin are initialized as empty arrays, and do not have the ability to use :Play()

  2. You never want to have “while true do” in a script because then it will run infinitely and you have no way to stop it.

I think you meant to create functions: Functions | Roblox Creator Documentation

2 Likes
  • You not specify what ProximityPrompt is triggered:

You

prompt.Triggered:Connect(function()

Example

Script.Parent.ProximityPrompt.Triggered:Connect(function()

@DemonLexe is wrong about the “While true do”

  • A “while do” loop can be stopped by adding a “Break” into the loop or it stop when the script is destroyed.
    In this case, you can use a “repeat until” to do it.

Example script

local Prompt = script.Parent.ProximityPrompt
local Part = script.Parent

Prompt.Triggered:Connect(function()
	if Prompt.ActionText == "Start" then
		Prompt.ActionText = "Stop"
		repeat
			wait(0.01)
			Part.CFrame = Part.CFrame * CFrame.fromEulerAnglesXYZ(0.6,0,0)
		until 
		Prompt.ActionText == "Start"
	elseif Prompt.ActionText == "Stop" then
		Prompt.ActionText = "Start"
		wait(0.025) -- Add +0.15s than the repeat time
		Part.Orientation = Vector3.new(0,0,0)
	end
end)

Example File
Prompt Spin.rbxl (23,4 Ko)

2 Likes

The reason why is probably because your not putting your loops in a coroutine and their isnt a Play function in one of your tables.

local partrotate = script.Parent
local spinning = false

local prompt = partrotate.ProximityPrompt

prompt.Triggered:Connect(function()
   spinning = not spinning
   prompt.ActionText = spinning and "Stop" or "Start"
end)

while wait(.01) do
   if not spinning then
      continue
   end

   partrotate.CFrame *= CFrame.fromEulerAnglesXYZ(.6,0,0)
end
2 Likes