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

When a player takes a local item, it goes into players backpack with server script?

Asked by
BiIinear 104
4 years ago

What I'm trying to do here is when a player takes an item that only they can see, a RemoteEvent is fired, and the server script makes the item go into the players backpack, therefore the player has a tool in their backpack on serverside.

Local Script

tweenservice = game:GetService("TweenService")
player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local Item = nil
local pos = nil
local transp = nil
local MaxDistance = 10

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.E and Mouse.Target.Parent:FindFirstChild('ItemValue') then
        Item = Mouse.Target
        pos = Item.Position
        transp = Item.Transparency
        if Item.Parent.ItemValue.Value == true then
            local magnitude = (Item.Position - player.Character.HumanoidRootPart.Position).magnitude
            if magnitude <= MaxDistance then
                if player.PlayerGui:FindFirstChild("Inventory").Frame.SlotFrame.Slot20.ItemNameValue.Value == '' then
                    Item.Parent.ItemValue.Value = false
                    script.Sound:Play()
                    local Info = TweenInfo.new(0.05)
                    local Goal = {}
                    Goal.CFrame = player.Character.HumanoidRootPart.CFrame
                    local Tween = tweenservice:Create(Item,Info,Goal)
                    Tween:Play()
                    if Item:FindFirstChild("Sound") then
                        Item.Sound:Play()
                    else
                    end
                    Item.Position = pos
                    Item.Transparency = transp
                    wait(0.05)
                    game.ReplicatedStorage.ItemToBackpack:FireServer(Item)
                else
                    Item = nil
                    print("Inventory full.")
                end
            end
        else
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Server Script

game.ReplicatedStorage.ItemToBackpack.OnServerEvent:Connect(function(player, Item)
    if Item.Parent:FindFirstChild("DroppedItem") then
        Item.CanCollide = false
        local ItemClone = game.Lighting[Item.Name]:Clone()
        ItemClone.Parent = player.Backpack
        ItemClone.LocalScript.Disabled = false
        Item.Parent:Destroy()
        game.ReplicatedStorage.ItemDestroy:FireServer(Item)
        print(Item.Name .. " Taken")
    else
        Item.Parent.ItemValue.Value = true
        local ItemClone = game.Lighting[Item.Name]:Clone()
        ItemClone.Parent = player.Backpack
        ItemClone.LocalScript.Disabled = false
        print(Item.Name .. " Taken")
    end
end)

I've tried sending the variable "Item" through scripts, but it comes out as "Workspace.ItemToBackpack:2: attempt to index local 'Item' (a nil value)" I'm not that familiar with RemoteEvents and RemoteFunctions, so excuse me for my misunderstanding. Help would be appreciated!

Answer this question