Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
xxaxxaz 42
2 years ago
Edited 2 years ago

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)
0
sorry about the grammer for the title, I had to shorten it so it was under 100 characters long. xxaxxaz 42 — 2y
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. Zeppelin0330 38 — 2y
0
yes but one of them say a if command in it to see if it was clicked. xxaxxaz 42 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago

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

Ad
Log in to vote
0
Answered by 2 years ago

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)

0
they are not indinticle xxaxxaz 42 — 2y
0
but thx for trying xxaxxaz 42 — 2y

Answer this question