Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Userinputservice LocalScript working in roblox studio, but not online?

Can anyone explain to me why this is happening?

local uis = game:GetService("UserInputService")





uis.InputBegan:connect(function(input,gp)
if input.UserInputType == Enum.UserInputType.Keyboard and (not gp) then
if input.KeyCode == Enum.KeyCode.Left then
script.Parent.Value = -1
end
end
end)

uis.InputEnded:connect(function(input,gp)
if input.UserInputType == Enum.UserInputType.Keyboard and (not gp) then
if input.KeyCode == Enum.KeyCode.Left then
script.Parent.Value = 0
end
end
end)

uis.InputBegan:connect(function(input,gp)
if input.UserInputType == Enum.UserInputType.Keyboard and (not gp) then
if input.KeyCode == Enum.KeyCode.Right then
script.Parent.Value = 1
end
end
end)


uis.InputEnded:connect(function(input,gp)
if input.UserInputType == Enum.UserInputType.Keyboard and (not gp) then
if input.KeyCode == Enum.KeyCode.Right then
script.Parent.Value = 0
end
end
end)

5 comments

0

Is this in a script or localscript

0

@XAXA This is a local script. It also says that in the title.

0

Try moving the localscript to workspace.

1

@leadfordead2 The localscript is not supposed to be in Workspace though.

0

I found out my problem! I just had two scripts utilizing userinput service asigned to the same button! I just need to put all of my code pertaining to that in one LocalScript. THANK YOU SO MUCH! @XAXA

1 answer

It all depends on where you have placed the Local Script.

A LocalScript will only run Lua code if it is a descendant of one of the following objects: 1. A Player's Backpack, such as a child of a Tool

2. A Player's Character model

3. A Player's PlayerGui

4. A Player's PlayerScripts

5. The ReplicatedFirst service

Additionally, you should really only be using one RBXScriptSignal for each event like so:

local uis = game:GetService("UserInputService")


uis.InputBegan:Connect(function(input,gp)
	if input.UserInputType == Enum.UserInputType.Keyboard and (not gp) then
		if input.KeyCode == Enum.KeyCode.Left then
			script.Parent.Value = -1
		end

		if input.KeyCode == Enum.KeyCode.Right then
			script.Parent.Value = 1
		end
	end
end)

uis.InputEnded:Connect(function(input,gp)
	if input.UserInputType == Enum.UserInputType.Keyboard and (not gp) then
		if input.KeyCode == Enum.KeyCode.Left then
			script.Parent.Value = 0
		end

		if input.KeyCode == Enum.KeyCode.Right then
			script.Parent.Value = 0
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.