I’m trying to make it so that I can move this visualizer part using E (forward) and Q (backward), but it isn’t working. It only works once and not multiple times, I also did it once and it said that the script exhausted itself, but I’m not completely sure what that means.
What should I do to fix this?
local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.
local Player = game.Players.LocalPlayer
local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local pianoModel = replicatedStorage:WaitForChild("Assets"):WaitForChild("Bombs"):WaitForChild("Piano")
local pianoVisualizer
local pianoMesh
local visualizerDistance = 0 -- The offset of the visualizer
local visualizeMoveConnection -- The heartbeat connection that moves the visualizer
function createVisualizer()
pianoVisualizer = Instance.new("Part")
pianoVisualizer.BrickColor = BrickColor.new("Persimmon")
pianoVisualizer.Size = pianoModel.Size
pianoVisualizer.Transparency = 0.5
pianoVisualizer.CanCollide = false
pianoVisualizer.Anchored = true
pianoMesh = Instance.new("SpecialMesh")
pianoMesh.MeshId = pianoModel.PianoMesh.MeshId
pianoMesh.Scale = pianoModel.PianoMesh.Scale
pianoMesh.Parent = pianoVisualizer
pianoVisualizer.Parent = Player.Character
visualizeMoveConnection = runService.Heartbeat:Connect(function()
pianoVisualizer.CFrame = Player.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -10 + -visualizerDistance)
end)
end
UserInputService.InputBegan:Connect(function(input, processed)
if not processed then
if UserInputService:IsKeyDown(Enum.KeyCode.Q) then
repeat visualizerDistance -= 1 until visualizerDistance >= 0 or not UserInputService:IsKeyDown(Enum.KeyCode.Q)
elseif UserInputService:IsKeyDown(Enum.KeyCode.E) then
repeat visualizerDistance += 1 until visualizerDistance <= 100 or not UserInputService:IsKeyDown(Enum.KeyCode.E)
end
end
end)
Tool.Equipped:Connect(function()
UserInputService.MouseIcon = MOUSE_ICON
createVisualizer()
end)
Tool.Unequipped:Connect(function()
UserInputService.MouseIcon = ""
pianoVisualizer:Destroy()
visualizeMoveConnection:Disconnect()
end)
Tool.Activated:Connect(function()
local cooldownTime = activateBombRemote:FireServer(Player)
repeat task.wait() until cooldownTime == false
UserInputService.MouseIcon = RELOADING_ICON
UserInputService.MouseIcon = MOUSE_ICON
end)
I’m not the best at coding, but I think “exhausted itself” means that it ran too many times without a wait, but you have a wait in there, so I’m not sure what the issue is. But once again, I’m pretty bad at coding lol.
AI response below
local MOUSE_ICON = "rbxasset://textures/GunCursor.png"
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png"
local Player = game.Players.LocalPlayer
local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local pianoModel = replicatedStorage:WaitForChild("Assets"):WaitForChild("Bombs"):WaitForChild("Piano")
local pianoVisualizer
local pianoMesh
local visualizerDistance = 0
function createVisualizer()
pianoVisualizer = Instance.new("Part")
pianoVisualizer.BrickColor = BrickColor.new("Persimmon")
pianoVisualizer.Size = pianoModel.Size
pianoVisualizer.Transparency = 0.5
pianoVisualizer.CanCollide = false
pianoVisualizer.Anchored = true
pianoMesh = Instance.new("SpecialMesh")
pianoMesh.MeshId = pianoModel.PianoMesh.MeshId
pianoMesh.Scale = pianoModel.PianoMesh.Scale
pianoMesh.Parent = pianoVisualizer
pianoVisualizer.Parent = Player.Character
end
-- Function to move the visualizer
function moveVisualizer(direction)
visualizerDistance = visualizerDistance + direction
pianoVisualizer.CFrame = Player.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -10 + -visualizerDistance)
end
-- Connect input events
UserInputService.InputBegan:Connect(function(input, processed)
if not processed then
if input.KeyCode == Enum.KeyCode.Q then
moveVisualizer(-1) -- Move backward
elseif input.KeyCode == Enum.KeyCode.E then
moveVisualizer(1) -- Move forward
end
end
end)
-- Call createVisualizer() when the tool is activated
Tool.Activated:Connect(createVisualizer)
repeat visualizerDistance -= 1 until visualizerDistance >= 0 or not UserInputService:IsKeyDown(Enum.KeyCode.Q) -- Here
elseif UserInputService:IsKeyDown(Enum.KeyCode.E) then
repeat visualizerDistance += 1 until visualizerDistance <= 100 or not UserInputService:IsKeyDown(Enum.KeyCode.E) -- And here
Just reading this tells me immedieltly that this will NOT at ALL fix what I’m trying to do. I don’t recommend trying to help people by using AI code, because it isn’t at all reliable all of the time.
You can remove the .1 from task.wait, i only put that there because without it, it’s moving way too fast.
If you remove the task.wait entirely, the part will just instantly teleport to it’s start/end
local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.
local Player = game.Players.LocalPlayer
local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local pianoModel = replicatedStorage:WaitForChild("Assets"):WaitForChild("Bombs"):WaitForChild("Piano")
local pianoVisualizer
local pianoMesh
local visualizerDistance = 0 -- The offset of the visualizer
local visualizeMoveConnection -- The heartbeat connection that moves the visualizer
function createVisualizer()
pianoVisualizer = Instance.new("Part")
pianoVisualizer.BrickColor = BrickColor.new("Persimmon")
pianoVisualizer.Size = pianoModel.Size
pianoVisualizer.Transparency = 0.5
pianoVisualizer.CanCollide = false
pianoVisualizer.Anchored = true
pianoMesh = Instance.new("SpecialMesh")
pianoMesh.Parent = pianoVisualizer
pianoVisualizer.Parent = Player.Character
visualizeMoveConnection = runService.Heartbeat:Connect(function()
pianoVisualizer.CFrame = Player.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -10 + -visualizerDistance)
end)
end
UserInputService.InputBegan:Connect(function(input, processed)
if not processed then
if UserInputService:IsKeyDown(Enum.KeyCode.Q) then
while UserInputService:IsKeyDown(Enum.KeyCode.Q) and visualizerDistance >= 0 do
task.wait(.1)
visualizerDistance -= 1
end
elseif UserInputService:IsKeyDown(Enum.KeyCode.E) then
while UserInputService:IsKeyDown(Enum.KeyCode.E) and visualizerDistance <= 100 do
task.wait(.1)
visualizerDistance += 1
end
end
end
end)
Tool.Equipped:Connect(function()
UserInputService.MouseIcon = MOUSE_ICON
createVisualizer()
end)
Tool.Unequipped:Connect(function()
UserInputService.MouseIcon = ""
pianoVisualizer:Destroy()
visualizeMoveConnection:Disconnect()
end)
Tool.Activated:Connect(function()
local cooldownTime = activateBombRemote:FireServer(Player)
repeat task.wait() until cooldownTime == false
UserInputService.MouseIcon = RELOADING_ICON
UserInputService.MouseIcon = MOUSE_ICON
end)