I need help with a script

so basically i need help figuring out whats not working and how to fix it, every time i go to the part and press “e” nothing happens but what is supposed to happen is it is supposed to disappear and appear on the gui that says "Packages: "

``local player = game.Players:GetPlayerFromCharacter(“Humanoid”)
local part = script.Parent
local bill = script.Parent.BillboardGui
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
if key == “e” and part.Touched == true then
local stats = player:findFirstChild(“leaderstats”)
if stats ~= nil then --If moneyholder exists then
local cash = stats:findFirstChild(“Package”) --Get money
cash.Value = cash.Value + 1 --increase amount of money by the number displayed here (500)
wait(0.1)
part:Destroy()
end
end) ```

1 Like

In my opinion, I would use :IsKeyDown
for instance this script my dev made:

UserInputService = game:GetService("UserInputService")
plr = script.Parent.Humanoid

while true do
	if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift or Enum.KeyCode.RightShift) then
		plr.WalkSpeed = 35
	else
		plr.WalkSpeed = 25
	end
	wait()
end

how would i put that into my script? because the way i did it, didn’t work.

here is how i put it

	if UserInputService:IsKeyDown(Enum.KeyCode.E) then
		local stats = player:findFirstChild("leaderstats")
		if stats ~= nil then --If moneyholder exists then
			local cash = stats:findFirstChild("Package") --Get money
			cash.Value  = cash.Value + 1 --increase amount of money by the number displayed here (500)
			wait(0.1)
		part:Destroy()
	else
		end
	end
end) ```

I am not sure, I think that looks correct though.

Do you get any errors in the output? If so, can you reply with an image of the errors?

If there aren’t any errors, try using print statements within your code to see what is/isn’t working.

lol you haven’t used UserInputService

Replace your code by this

local player = game:GetService("Players").LocalPlayer
local part = script.Parent
local bill = script.Parent.BillboardGui
local UIS = game:GetService("UserInputService")
local mouse = player:GetMouse()

local touched = false

part.Touched:Connect(function(hit)
     if hit.Parent:FindFirstChild("Humanoid") then
      touched = true
end)

part.TouchEnded:Connect(function(hit)
     if hit.Parent:FindFirstChild("Humanoid") then
      touched = false
end)


UIS.InputBegan:Connect(function(key)
    if key.KeyCode = Enum.KeyCode.E and Touched == true then
     local stats = player:findFirstChild(“leaderstats”)
        if stats ~= nil then --If moneyholder exists then -- these codes are yours
             local cash = stats:findFirstChild(“Package”) --Get money
             cash.Value = cash.Value + 1 --increase amount of money by the number displayed here (500)
             wait(0.1)
             part:Destroy()
        end
    end) 
end)


1 Like

KeyDown is deprecated, you have to use UIS or CAS (I prefer CAS for mobile buttons)