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

why can I pick up the wood with any keycode when I just want it to be the keycode F?

I was following a tutorial but I changed some things so it is usable on mobile, so then players can click the wood to to pick it up, it works perfectly exept that it can get picked up with any keycode instead of just the keycode F.

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickupItem = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("PickupItem")
local pickupKey = "F"

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local item = mouse.Target
local distanceFromItem = nil

local PlayerGui = player:WaitForChild("PlayerGui")
local PickupInfoGui = PlayerGui:WaitForChild("PickupInfoGui")

UIS.InputBegan:Connect(function(input)
	if mouse.Target and input.KeyCode == Enum.KeyCode.F then
		if mouse.Target:FindFirstChild("Pickable") then
			local item = mouse.Target
			PickupInfoGui.Adornee = item
			PickupInfoGui.ObjectName.Text = item.Name
			PickupInfoGui.Enabled = true
		else
			PickupInfoGui.Adornee = nil
			PickupInfoGui.Enabled = false
		end
	end
end)


UIS.InputEnded:Connect(function(input)
	if mouse.Target then
		if mouse.Target:FindFirstChild("Pickable") then
			item = mouse.Target
			if item then
				distanceFromItem = player:DistanceFromCharacter(item.Position)
				if item.Touched then --if item clicked, I feel like it is here.
					PickupItem:FireServer(item)
				elseif input.KeyCode == Enum.KeyCode.F then --if player type F
					PickupItem:FireServer(item)
				end
			end
		end
	end
end)

2 comments

0

Why are you not useing a proximity promt???

0

because that is not what I need but I will try to make one

2 answers

✓ Accepted answer
UIS.InputChanged:Connect(function(input)
    if mouse.Target then
        if mouse.Target:FindFirstChild("Pickable") then
            local item = mouse.Target
            PickupInfoGui.Adornee = item
            PickupInfoGui.ObjectName.Text = item.Name
            PickupInfoGui.Enabled = true
        else
            PickupInfoGui.Adornee = nil
            PickupInfoGui.Enabled = false
        end
    end
end)

You have a variable, pickupkey but you never use it. It's likely you didn't finish the tutorial or the tutorial was half-assed.

Either way, specify which input you want like this:

UIS.InputBegan:Connect(function(input)
    if mouse.Target and input.KeyCode == Enum.KeyCode.F then
        if mouse.Target:FindFirstChild("Pickable") then
            local item = mouse.Target
            PickupInfoGui.Adornee = item
            PickupInfoGui.ObjectName.Text = item.Name
            PickupInfoGui.Enabled = true
        else
            PickupInfoGui.Adornee = nil
            PickupInfoGui.Enabled = false
        end
    end
end)

Same goes for InputEnded, understand what I did visually and replicate it. Also, remove line 36

Side note: I also changed it to InputBegan vs InputChanged there is a minor difference that you can search up but it shouldn't impact you very hard.

Larger edit:

UIS.InputEnded:Connect(function(input)
    if mouse.Target then
        if mouse.Target:FindFirstChild("Pickable") then
            item = mouse.Target
            if item then
                distanceFromItem = player:DistanceFromCharacter(item.Position)
                if item.Touched then --if item clicked, I feel like it is here.
		if input.KeyCode == Enum.KeyCode.F then
			PickupItem:FireServer(item)
		end
            end
        end
    end
end)

1 comment

0

I did finish the tutorial but I changed the [inputkey] to F just to make sure that was not the problem

All the codes you need to make what you want will be included here.

Basic proximity prompt

workspace.Part.ProximityPrompt.Triggered:Connect(function(player)
    print("The user interacted with me!")
end)

Checking which device a player is in (i think this will work... I found this on the internet

local UIS = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")

if UIS.TouchEnabled and not UIS.KeyboardEnabled and not UIS.MouseEnabled
   and not UIS.GamepadEnabled and not GuiService:IsTenFootInterface() then

   -- mobile device
else

 --pc

end

5 comments

0

thx

0

does not work error: Players.xxaxxaz.PlayerScripts.PickupManager:9: attempt to index nil with 'Position' - Client - PickupManager:9

0

I think it is because it does not auto clone to all the blocks that need it.

0

I made a succesful auto clone and it still does not work

0

hmm

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