Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does this Value Change script not work nor print anything?

Asked by 6 years ago

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

01local UIS = game:GetService("UserInputService")
02local tool = script.Parent
03 
04UIS.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
11end)

Local Script

01local rep = game.ReplicatedStorage
02local maxpower = 120
03local minpower = 0
04 
05rep.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
12end)
13 
14rep.Events.PowerChangeE.OnServerEvent:Connect(function(player,tool,key)
15    if key == Enum.KeyCode.E and tool.Power.Value > maxpower then
View all 21 lines...

Server Script

0
Why are you checking the key pressed on the server? Never do that. User#24403 69 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The reason is your conditional statements.

The way you have formatted them makes this confusing to see. This is what you effectively have

1if 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
6end

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:

1if IO.KeyCode = Enum.KeyCode.Q then
2    game.ReplicatedStorage.Events.PowerChangeQ:FireServer(tool, Enum.KeyCode)
3elseif IO.KeyCode = Enum.KeyCode.E then
4    game.ReplicatedStorage.Events.PowerChangeE:FireServer(tool, Enum.KeyCode)
5end

If this helped, please upvote. If this fixed your issue, please set this as the correct answer!

1
This is actually incorrect. if and elseif are in one block. This means that there shouldn't be two end keywords. There should only be one end keyword. saSlol2436 716 — 6y
0
Thank you! Amended plasmascreen 143 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Mistakes

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)

LocalScript

01-- Services
02local UserInputService = game:GetService("UserInputService")
03local ReplicatedStorage = game:GetService("ReplicatedStorage")
04 
05-- Tool Data
06local Tool = script.Parent
07 
08-- Network Data
09local Events = ReplicatedStorage:WaitForChild("Events")
10local PowerChangeQ = Events:WaitForChild("PowerChangeQ")
11local 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 
View all 37 lines...

Server Script (Or Normal Script)

01-- Services
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03-- Events
04local Events = ReplicatedStorage:WaitForChild("Events")
05local PowerChangeQ = Events:WaitForChild("PowerChangeQ")
06local PowerChangeE = Events:WaitForChild("PowerChangeE")
07 
08local MaxPower = 120
09local 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
View all 43 lines...

Answer this question