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

Why does the Part not move up While holding down a button?

So currently i am developing a drone. I Made a script that Should move the part up while holding down E.

heres the script:

local Drone = script.Parent -- Yes this will be a drone
local UIS = game:GetService("UserInputService")

local HoldingEkey = false

UIS.InputBegan:Connect(function(InputObject) -- Input began
	if (InputObject.KeyCode == Enum.KeyCode.E) then
		print("Input Began") -- This print somehow does not print
		HoldingEkey = true -- true
		while HoldingEkey do
			Drone.Position = Drone.Position + Vector3.new(0,0,-0.1,0) -- position
			print("should move") -- This does not print either
			wait(0.1)	
		end
	end
end)

UIS.InputEnded:Connect(function(InputObject) -- Input ended
	if (InputObject.KeyCode == Enum.KeyCode.E) then
		HoldingEkey = false
		print("Input Ended")
	end
end)

somehow It does not work. Theres no errors And i have not anchored it. Am i doing something wrong?

Would respect all help

8 comments

0

Is it a Normal script or a Local script?

0

@BestCreativeBoy Its a Normal script. Its parented in The Part

0

Make sure to use a Local Script when using UserInputService as it is exclusively to Local script. And Local scripts won't work in worksapce.

0

*workspace

0

@BestCreativeBoy Ikr but Where should i parent it though

0

@BestCreativeBoy Where should i parent it?

0

@BestCreativeBoy Nvm fixed it by parenting it to StarterGui

0

But, the issue that will occur that it will move only on your screen and not on other player's screen.

1 answer

✓ Accepted answer

Well, as you said, it's a Normal/Server script. But, UserInputService is exclusive to Local script only.

So, what's the solution?

Well, you can use RemoteEvents to contact between Server and Client. This is what you can do :

-- Local script [Keep it in StarterCharacterScripts or StarterGui]
local UIS = game:GetService("UserInputService")
local RS = game:GetService('ReplicatedStorage')
local RE = RS:WaitForChild("REMOTE_EVENT") -- Your Remote Event

UIS.InputBegan:Connect(function(InputObject) -- Input began
    if (InputObject.KeyCode == Enum.KeyCode.E) then
        print("Input Began")
        RE:FireServer() -- A command to contact with server using RemoteEvents
    end
end)

UIS.InputEnded:Connect(function(InputObject) -- Input ended
    if (InputObject.KeyCode == Enum.KeyCode.E) then
        RE:FireServer() 
        print("Input Ended")
    end
end)

-- Normal script [Keep it in ServerScriptService]
local RS = game:GetService('ReplicatedStorage')
local RE = RS:WaitForChild("REMOTE_EVENT")
local Drone = workspace.Drone -- An assumed location of your drone in workspace

local HoldingEkey = RS.HoldEKey -- Add a bool value in ReplicatedStorage and name it 'HoldEKey'

RE.OnServerEvent:Connect(function(player) -- 'player' is the one who fired to the server

	if HoldingEkey.Value then -- If the value was true
		HoldingEkey.Value = false -- Make it false
	else
		HoldingEkey.Value = true -- If value was false, make it true
	end

	 while HoldingEkey.Value do
            Drone.Position = Drone.Position + Vector3.new(0,0,-0.1,0) -- position
            print("should move")
            wait(0.1)   
        end
end)

Comment down if you want explanation of something!

Lemme know if it helps!

2 comments

0

Ha, Best, you read it right of my mind! I would upvote if I had enough rep! Perfect script, perfect response, great overall!

0

May be good in the future. This will be a game that is only 1 player max but i will use this in the future i promise. Thanks :)

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