So I want it so that when I press E it should add 5 to value. But if i press Q it should take away 5 from value
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local tool = script.Parent |
03 |
04 | UIS.InputBegan:Connect( function (IO) |
05 | if IO.KeyCode = = Enum.KeyCode.Q then |
06 | game.ReplicatedStorage.Events.PowerChangeQ:FireServer(tool,Enum.KeyCode.Q) |
07 | if IO.KeyCode = = Enum.KeyCode.E then |
08 | game.ReplicatedStorage.Events.PowerChangeE:FireServer(tool,Enum.KeyCode.E) |
09 | end |
10 | end |
11 | end ) |
Local Script
01 | local rep = game.ReplicatedStorage |
02 | local maxpower = 120 |
03 | local minpower = 0 |
04 |
05 | rep.Events.PowerChangeQ.OnServerEvent:Connect( function (player,tool,key) |
06 | if key = = Enum.KeyCode.Q and tool.Power.Value > minpower then |
07 | wait() |
08 | tool.Power.Value = tool.Power.Value - 5 |
09 | wait() |
10 | print (tool.Power.Value) |
11 | end |
12 | end ) |
13 |
14 | rep.Events.PowerChangeE.OnServerEvent:Connect( function (player,tool,key) |
15 | if key = = Enum.KeyCode.E and tool.Power.Value > maxpower then |
Server Script
The reason is your conditional statements.
The way you have formatted them makes this confusing to see. This is what you effectively have
1 | if IO.KeyCode = Enum.KeyCode.Q then |
2 | game.ReplicatedStorage.Events.PowerChangeQ:FireServer(tool, Enum.KeyCode) |
3 | if IO.KeyCode = Enum.KeyCode.E then |
4 | game.ReplicatedStorage.Events.PowerChangeE:FireServer(tool, Enum.KeyCode) |
5 | end |
6 | end |
This means that you're not checking if the key pressed was 'E' unless the letter 'Q' was the one being pressed. This is not going to work for obvious reasons. You should split this into an if and an elseif like so:
1 | if IO.KeyCode = Enum.KeyCode.Q then |
2 | game.ReplicatedStorage.Events.PowerChangeQ:FireServer(tool, Enum.KeyCode) |
3 | elseif IO.KeyCode = Enum.KeyCode.E then |
4 | game.ReplicatedStorage.Events.PowerChangeE:FireServer(tool, Enum.KeyCode) |
5 | end |
If this helped, please upvote. If this fixed your issue, please set this as the correct answer!
Some mistakes you have done in your code are, not using Key.KeyCode. That was the major problem in your code. I edited your code to match UpperCamelCase(like that). In addition, I have made (and/or have some plans) the code a bit more efficient(for both scripts)
01 | -- Services |
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 |
05 | -- Tool Data |
06 | local Tool = script.Parent |
07 |
08 | -- Network Data |
09 | local Events = ReplicatedStorage:WaitForChild( "Events" ) |
10 | local PowerChangeQ = Events:WaitForChild( "PowerChangeQ" ) |
11 | local PowerChangeE = Events:WaitForChild( "PowerChangeE" ) |
12 |
13 | -- I like putting things in KeyInputs so I won't have to put a lot of if-statements in my InputBegan |
14 | -- event |
15 |
01 | -- Services |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 | -- Events |
04 | local Events = ReplicatedStorage:WaitForChild( "Events" ) |
05 | local PowerChangeQ = Events:WaitForChild( "PowerChangeQ" ) |
06 | local PowerChangeE = Events:WaitForChild( "PowerChangeE" ) |
07 |
08 | local MaxPower = 120 |
09 | local MinPower = 0 |
10 |
11 | -- I think you can make all these remote events into |
12 | -- one remote event with if statements checking what key it is |
13 | --[[ |
14 | KeyRemote.OnServerEvent:Connnect(function(Player,Tool,Key) |
15 | if Key == Enum.KeyCode.Q and Power.Value > MinPower then |