UserInputService Problems

Hey. My name is DivScripts.

I was trying to make a spell system.

Now, I created a LocalScript with this script in it:

game:GetService("UserInputService").InputBegan:Connect(function(input,progress)
	if progress == false or nil then
		print(input)
		game.ReplicatedStorage.SpellRemoteEvents.KeyBindSpell:FireServer(input,game.Players.LocalPlayer:GetMouse(),game.Players.LocalPlayer:GetMouse().Target)
	end
end)

In the LocalScript it prints the input which is InputObject. After I connected from the ServerScript to the KeyBindSpell : Remote Event it prints the nil:

game.ReplicatedStorage.SpellRemoteEvents.KeyBindSpell.OnServerEvent:Connect(function(player,input,mouse,mousetarget)
	print(input)

Anyone who knows how to fix this?

/

Problem 2:

After the problem up got fixed, is there any way to use like string in a code?

For example:

if input.KeyCode == Enum.KeyCode["STRING"]

Is there any way to make it like this? Or like how to convert a string into code.

Best Regards,
DivScripts

1 Like

You need to check which button player is pressing from the client and then fire the remote, like this.

local key = "E" -- button

game:GetService("UserInputService").InputBegan:Connect(function(input,typing)
	if typing == true then
		return
	end

	if input.InputBegan == Enum.KeyCode[key] then
		game.ReplicatedStorage.SpellRemoteEvents.KeyBindSpell:FireServer(game.Players.LocalPlayer:GetMouse(),game.Players.LocalPlayer:GetMouse().Target)
	end
end)

But I don’t want to check this in LocalScript. Because, when I put the table in LocalScript too, exploiter will getting acess on it…

You can’t check which button player is pressing on server-side anyway.

You should do a sanity check.

I- I haven’t really understand this sanity check. >This gives the exploiter still acess to my script when I put the table in it.

What script? wwwwwwwwwwwwwwwwwww

Nvm. I made it now so, I created a table with just the all the letters in the LocalScript. Then I check after a button got pressed if the button in the table is and if yes, fire it to the Server Script.

local key = "E" -- button

game:GetService("UserInputService").InputBegan:Connect(function(input,typing)
	if typing == true then
		return
	end

	if input.InputBegan == Enum.KeyCode[key] then
		game.ReplicatedStorage.SpellRemoteEvents.KeyBindSpell:FireServer(game.Players.LocalPlayer:GetMouse(),game.Players.LocalPlayer:GetMouse().Target)
	end
end)

You mean this? from what I see is just check if player is pressing the right key or not and then fire the remote.

1 Like

If your script isn’t in StarterPlayerScripts, it won’t work. UserInputService works in StarterPlayerScripts and in a local script only.