Ad
Log in to vote
0

After passing the parameter from local to a server script, it becomes nil. Can you help me out?

Asked by
Padugz 40
2 years ago

Hello everyone! So I am making a game similar to Survival 303 (dang, nostalgia feels). I have my forage tool, which forages an item that has a: ClickDetector and forageable value (boolean value). So when all criteria are met, the forage tool will then pass the parameter 'Target' (which is the name for the mouse.Target), to a server script through a remote event. The server script will then find the corresponding item from the player's inventory (server-side), which will then add 1 to its value. After that, the server will fire another remote event to change the value on the player's inventory (client-side).

Now, I also have my backpack gui. This gui, whenever an item's value (located on the player's inventory - client side) changes, the corrresponding item's gui will also change (either visible or not). Note that, before showing the item's gui, it will not only check the player's inventory in the client-side, but also the player's inventory on the server-side. This is to avoid having any discrepancy or to avoid malicious attacks (note that FilteringEnabled was already on, that's why I use remote events.).

In the same backpack gui, I also have this button called "drop button". This drop button checks if the currently active item's gui has a value in the inventory (client and server-side) more than 0. If it has, it will clone the "physical item" and be parented to the workspace (where my 'initial' item was located before foraging it.) Now, when I use the same forage tool, it still picks-up the 'dropped item', because it still meets all the criteria, but here comes the problem. Before the forage tool fires the remote event to the server, the 'Target' is still present, but when the server picks up the 'Target', it becomes nil. Can you help me out on this one?

Found below are the codes. Note that I did not put the section where I set-up my variables because it will make this post lengthy

A. FORAGE TOOL

Tool_Forage.Activated:Connect(function()
    local Target = Mouse.Target
    if Tool_Usage then
        Tool_Usage = false
        if Target then
            if Target:FindFirstChild("ClickDetector") and Target:FindFirstChild("Forageable") then
                if Humanoid.Health > 0 then
                    local Target_Range = Target:FindFirstChild("ClickDetector").MaxActivationDistance
                    local Humanoid_Position = Humanoid.Parent:FindFirstChild("HumanoidRootPart").Position
                    if (Humanoid_Position - Target.Position).Magnitude <= Target_Range then
                        Target_Range = 0
                        Animation_Forage_Load:Play()
                        Humanoid.WalkSpeed = 0
                        Humanoid.JumpPower = 0
                        wait(Animation_Forage.Timer.Value)
                        Animation_Forage_Load:Stop()
                        toInventory_Server:FireServer(Target)
                        Humanoid.WalkSpeed = 16
                        Humanoid.JumpPower = 50
                        Tool_Usage = true
                    else
                        print("Target out of range.")
                        Tool_Usage = true
                    end
                else
                    print("Clicker dead.")
                    Tool_Usage = true
                end
            else
                print("Target not forageable nor craftable.")
                Tool_Usage = true
            end
        else
            print("Target not clickable.")
            Tool_Usage = true
        end
    end
end)

B. INVENTORY SERVER:

local function Inventory_Server_Changer(player, Target)
    print(Target)
    local Inventory_Server = ServerStorage:FindFirstChild("Inventory_" .. player.Name)
    local Target_Name = tostring(Target)
    for i, Item_Server in pairs(Inventory_Server:GetChildren()) do
        if Item_Server.Name == Target_Name then
            Item_Server.Value += 1
            local Item_Server_Value = Item_Server.Value
            toInventory_Player:FireClient(player, Target, Item_Server_Value)
            Target:Destroy()
            print(tostring(Item_Server.Name) .. "'s value in the Inventory_Server is changed by" .. tostring(Item_Server_Value))
        else
            print(tostring(Item_Server.Name) .. " is not the item.")
        end
    end

end

toInventory_Server.OnServerEvent:Connect(Inventory_Server_Changer)

Take note that in the line print(Target) of the Inventory Server is where the error occurs. It prints out nil.

0
Is it possible that Target is destroyed while you do wait(Animation_Forage.Timer.Value)? Or could it be a Part that is created by the client? gskw 1046 — 2y
1
Are you sure Target isn't a client-sided part? Amiaa16 3227 — 2y
0
@gskw, no, the Target is not destroyed. The dropped item, which is the new Target, is actually a cloned from a Replicated Storage where all the parts are store (for replication for purposes). So item reserve from the Replicated Storage was cloned (after checking the item's values on Player's Inventory on the server and on the client), and then Parented to the Workspace. Padugz 40 — 2y
0
@Kiriot22, the drop gui on the backpack of the player initiated the clone process. When clicked, it will prompt the server to check the inventory, return, then the local script within the drop gui will clone it. I think you mean to place the clone script on the server right? Not on a local script? I'll try it out! Padugz 40 — 2y

1 answer

Log in to vote
0
Answered by
Padugz 40
2 years ago

Okay, Kiriot22's answer worked. It is because the tool's local script pick-ups whatever part that met the criteria - whether existing on the client or on the whole server. But, the problem lies that the server script only accepts parameters, or in the case - Target, that is present in the server. Note that, I failed to consider this because on the local player's workspace, the item is existing. With that, I thought that since workspace is server-sided, the target will also exist in the workspace's of the other players. Once again, thank!

Ad

Answer this question