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

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

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

local UIS = game:GetService("UserInputService")
local tool = script.Parent

UIS.InputBegan:Connect(function(IO)
	if IO.KeyCode == Enum.KeyCode.Q then
		game.ReplicatedStorage.Events.PowerChangeQ:FireServer(tool,Enum.KeyCode.Q)
	if IO.KeyCode == Enum.KeyCode.E then
		game.ReplicatedStorage.Events.PowerChangeE:FireServer(tool,Enum.KeyCode.E)
	end
	end
end)

Local Script

local rep = game.ReplicatedStorage
local maxpower = 120
local minpower = 0

rep.Events.PowerChangeQ.OnServerEvent:Connect(function(player,tool,key)
	if key == Enum.KeyCode.Q and tool.Power.Value > minpower then
		wait()
	tool.Power.Value = tool.Power.Value - 5
		wait()
		print(tool.Power.Value)
	end
end)

rep.Events.PowerChangeE.OnServerEvent:Connect(function(player,tool,key)
	if key == Enum.KeyCode.E and tool.Power.Value > maxpower then
		wait()
	tool.Power.Value = tool.Power.Value + 5
		wait()
		print(tool.Power.Value)
	end
end)

Server Script

1 comment

0

Why are you checking the key pressed on the server? Never do that.

2 answers

The reason is your conditional statements.

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

if IO.KeyCode = Enum.KeyCode.Q then
	game.ReplicatedStorage.Events.PowerChangeQ:FireServer(tool, Enum.KeyCode)
	if IO.KeyCode = Enum.KeyCode.E then
		game.ReplicatedStorage.Events.PowerChangeE:FireServer(tool, Enum.KeyCode)
	end
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:

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

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

2 comments

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.

0

Thank you! Amended

##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

-- Services
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Tool Data
local Tool = script.Parent

-- Network Data
local Events = ReplicatedStorage:WaitForChild("Events")
local PowerChangeQ = Events:WaitForChild("PowerChangeQ")
local PowerChangeE = Events:WaitForChild("PowerChangeE")

-- I like putting things in KeyInputs so I won't have to put a lot of if-statements in my InputBegan
-- event

local KeyInputs = {
	[Enum.KeyCode.Q] = PowerChangeQ;
	[Enum.KeyCode.E] = PowerChangeE
}

function OnInputBegan(Key,GameProcessedEvent)
	-- Make sure that the player isn't pressing "/" to open chat 
	-- or "tab" to toggle the player list
	-- by checking if GameProcessedEvent is true

	-- These are gaurd statements. I prefer using them instead of if-blocks.
	if GameProcessedEvent then return end 
    	if not KeyInputs[Key.KeyCode] then return end

	-- A huge mistake you are doing is you are not doing Key.KeyCode.
	-- Key.KeyCode will actually return the Key, but, Key
	-- has other things you can use.

	KeyInputs[Key.KeyCode]:FireServer(Tool,Key.KeyCode)
end

UserInputService.InputBegan:Connect(OnInputBegan)

##Server Script (Or Normal Script)

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Events
local Events = ReplicatedStorage:WaitForChild("Events")
local PowerChangeQ = Events:WaitForChild("PowerChangeQ")
local PowerChangeE = Events:WaitForChild("PowerChangeE")

local MaxPower = 120
local MinPower = 0

-- I think you can make all these remote events into 
-- one remote event with if statements checking what key it is
--[[
	KeyRemote.OnServerEvent:Connnect(function(Player,Tool,Key)
		if Key == Enum.KeyCode.Q and Power.Value > MinPower then
			-- Code
		elseif Key == Enum.KeyCode.E and Power.Value > MaxPower then
			-- Code
		end
	end)
--]]

PowerChangeQ.OnServerEvent:Connect(function(Player,Tool,Key)   
      local Power = Tool:WaitForChild("Power")
 
       if Key == Enum.KeyCode.Q and Power.Value > MinPower then
             wait()
             Power.Value = Power.Value - 5
             wait()
             print(Power.Value)
       end
end)

PowerChangeE.OnServerEvent:Connect(function(Player,Tool,Key)
       local Power = Tool:WaitForChild("Power")

       if Key == Enum.KeyCode.E and Power.Value > MaxPower then
          wait()
          Power.Value = Power.Value + 5
          wait()
          print(Power.Value)
      end
end)

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