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

gives me 2 wood instead of 1 wood, I need it to be 1 wood?

I was following a tutorial with a bit of changes, but when I pressed F it would five me double the amount of wood, and when I press anything else then F or click the wood I get the normal amount. I just want it to give me the normal amount. btw there is not a error.

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 PlayerGui = player:WaitForChild("PlayerGui")
local PickupInfoGui = PlayerGui:WaitForChild("PickupInfoGui")

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)

--I think the problem is these 2 if statements.
UIS.InputEnded:Connect(function(input) --this one has a enum keycode for F
	if input.KeyCode == Enum.KeyCode.F then
		if mouse.Target then
			if mouse.Target:FindFirstChild("Pickable") then
				local item = mouse.Target
				if item then
					local distanceFromItem = player:DistanceFromCharacter(item.Position)
					if distanceFromItem < 30 then
					PickupItem:FireServer(item)
				
					end
				end
			end
		end
	end
end)
UIS.InputEnded:Connect(function()--this one has a if item.Touched
	if mouse.Target then
		if mouse.Target:FindFirstChild("Pickable") then
			local item = mouse.Target
			if item then
				if item.Touched then
					local DistanceFromItem = player:DistanceFromCharacter(item.Position)
					if DistanceFromItem < 30 then
						PickupItem:FireServer(item)
					end
				end
			end
		end
	end
end)

3 comments

0

sorry about the grammer for the title, I had to shorten it so it was under 100 characters long.

0

You seem to be firing the even twice UIS.input ended event twice as line 27-42 is fired and then likes 43-57 fire and since the checks are the same its gonna pass and thus the collecting event is fired twice so your collection doubles.

0

yes but one of them say a if command in it to see if it was clicked.

2 answers

✓ Accepted answer

Try adding an F button on mobile so then on the screen for mobile players, instead of having to click it they can just click the extra F

Lines 27- 42 and 43-57 are fired at the same time and so your checks in both pass since they are identical you would have both events fire which would as a result double the collection. I suggest you to remove lines 43-57 and change your lines 27 -42 to:

UIS.InputEnded:Connect(function(input)
	    if input.KeyCode == Enum.KeyCode.F or input.UserInputType = Enum.UserInputType.MouseButton1 then
	        if mouse.Target then
            if mouse.Target:FindFirstChild("Pickable") then
	                local item = mouse.Target
	                if item then
	                    local distanceFromItem = player:DistanceFromCharacter(item.Position)
                 if distanceFromItem < 30 then
	                    PickupItem:FireServer(item)
	 
	                    end
	                end
	            end
	        end
	    end
	end)

2 comments

0

they are not indinticle

0

but thx for trying

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