I really need help with this script. I’m not a scripter in any way so please don’t destroy me in the replies lol (also I just used a tutorial). So basically, I have a part that I want to be able to move using the arrow keys while have a tool equipped. This is the localscript I have written using a tutorial, however it doesn’t work and nothing below comes up in the output. Please help.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local db = false
local uis = game:GetService("UserInputService")
local movingpart = workspace.Union
script.Parent.Activated:Connect(function()
if db == false and hum then
db = true
hum.WalkSpeed = 0
print("Script activated")
else
db = false
hum.WalkSpeed = 16
print("Script deactivated")
end
end)
uis.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.Left and db == true then
movingpart.Position = movingpart.Position + Vector3.new(-3, 0, 0)
print("Moving part left")
elseif input.KeyCode == Enum.KeyCode.Right and db == true then
movingpart.Position = movingpart.Position + Vector3.new(3, 0, 0)
print("Moving part right")
end
end)
From experience alone I can attest to the fact that player.CharacterAdded:Wait() does not work on a tool. The reason why it prints nothing is because when it gets to
local hum = char:WaitForChild("Humanoid")
it has no reference for the “character” because the character does not exist, or so I would suspect. That or it’s stuck in a loop elsewhere. Instead of what you have for the character, replace local char = player.Character or player.CharacterAdded:Wait() with
local char
repeat char = player.Character wait(1) until char ~= nil
I replaced the local char part but it still doesn’t work. Also, I just want to let you know that I have another script which is linked to the same part (it makes the part rotate infinitely). Could that have something to do with this?
Has to do with your gameProcessedEvent if statement. When you press the arrow keys it returns true meaning the game recognized the input and since it did your if statement tells the function to stop.
Add not before gameProcessedEvent then return end and it should hopefully fix your problem.
No your humanoid and character should be defined perfectly fine. My other guess would be that it has something to do with your debounce variable. Try printing db above your KeyCode if statements to see if something is off.
Did you fix your gameProcessedEvent if statement? If not print that variable above that if statement to see what’s going on or temporarily remove it.
uis.InputBegan:Connect(function(input, gameProcessedEvent)
print(gameProcessedEvent) -- check to see if game recognizes input
if not gameProcessedEvent then return end
print(db) -- check to see what variable is set to
if input.KeyCode == Enum.KeyCode.Left and db == true then
movingpart.Position = movingpart.Position + Vector3.new(-3, 0, 0)
print("Moving part left")
elseif input.KeyCode == Enum.KeyCode.Right and db == true then
movingpart.Position = movingpart.Position + Vector3.new(3, 0, 0)
print("Moving part right")
end
end)
My guess is it most likely has to do something with your script being even enabled then or where it is placed.
script.Parent.Activated:Connect(function()
if db == false and hum then
db = true
hum.WalkSpeed = 0
print("Script activated")
else
db = false
hum.WalkSpeed = 16
print("Script deactivated")
end
end)
First: is the script activated?
Second: what is the ancestor/parent (is it in playerscripts, startergui, etc.)?
Ok now I’m nervous because I’m scared it was a fault on my end lol
Here’s my updated code:
local player = game.Players.LocalPlayer
local char
repeat char = player.Character wait(1) until char ~= nil
local hum = char:WaitForChild("Humanoid") wait(1) until char ~= nil
local db = false
local uis = game:GetService("UserInputService")
local movingpart = workspace.Union
script.Parent.Activated:Connect(function()
if db == false and hum then
db = true
hum.WalkSpeed = 0
print("Script activated")
else
db = false
hum.WalkSpeed = 16
print("Script deactivated")
end
end)
uis.InputBegan:Connect(function(input, gameProcessedEvent)
print(gameProcessedEvent) -- check to see if game recognizes input
if not gameProcessedEvent then return end
print(db) -- check to see what variable is set to
if input.KeyCode == Enum.KeyCode.Left and db == true then
movingpart.Position = movingpart.Position + Vector3.new(-3, 0, 0)
print("Moving part left")
elseif input.KeyCode == Enum.KeyCode.Right and db == true then
movingpart.Position = movingpart.Position + Vector3.new(3, 0, 0)
print("Moving part right")
end
end)
No worries! This happens often with programming, nothing to feel bad about.
I would revert this section of code:
local char
repeat char = player.Character wait(1) until char ~= nil
local hum = char:WaitForChild("Humanoid") wait(1) until char ~= nil
back to this, because it worked fine before, so there was no need to change it:
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
I also forgot to incorporate your db variable in my script, lol. Everything still works, but your db variable is inflicting. What is its purpose exactly?
local player = game.Players.LocalPlayer
local char
local tool = script.Parent
repeat
wait(1)
char = player.Character
until char
local hum = char:WaitForChild("Humanoid")
local db = false
local uis = game:GetService("UserInputService")
local movingpart = workspace.Union
tool.Activated:Connect(function()
if db == false and hum then
db = true
hum.WalkSpeed = 0
print("Script activated")
elseif hum then
db = false
hum.WalkSpeed = 16
print("Script deactivated")
end
end)
uis.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then return end -- suposed to be like that because arrow left and right keys are events handled by roblox.
if input.KeyCode == Enum.KeyCode.Left and db == true then
movingpart.Position = movingpart.Position + Vector3.new(-3, 0, 0)
print("Moving part to the left")
elseif input.KeyCode == Enum.KeyCode.Right and db == true then
movingpart.Position = movingpart.Position + Vector3.new(3, 0, 0)
print("Moving part to the right")
end
end)
Edit:
There is gameProcessedEvent in uis.inputBegan and gameProcessedEvent is equal to true when roblox is handling it’s events so…
Arrow keys are roblox handled events that’s why it won’t work and your code didn’t work too before