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

gun not goning in players backpack mouse1click??

script.Parent.MouseButton1Click:connect(function()
    if game.Players.LocalPlayer.Equipment.PrimaryGun.Value == "Startergun" then
        game.ServerStorage.Guns.Startergun:Clone().Parent = game.Players.LocalPlayer.Backpack
    else
        if game.Players.LocalPlayer.Equipment.PrimaryGun.Value == "Rank2Gun" then
        game.ServerStorage.Guns.Rank2:Clone().Parent = game.Players.LocalPlayer.Backpack
    end
    end
    end)

its in a local script all the vaules are right works in studio but not in game

1 answer

✓ Accepted answer

###The problem with your script is you're attempting to access ServerStorage from the client, when the service is server-side only, hence the name ServerStorage. Also, you should be cloning on the server as the changes will not replicate if all under a local script. You'll need a RemoteEvent to access ServerStorage.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CloneTool = ReplicatedStorage:WaitForChild("CloneTool") -- remote event
local plr = game:GetService("Players").LocalPlayer 
local primaryGun = plr.Equipment.PrimaryGun -- getting primarygun object


script.Parent.MouseButton1Click:Connect(function() -- :connect is deprecated, use :Connect
    CloneTool:FireServer(primaryGun.Value) -- make sure the Value is spelt like the gun name
end)

###Server code, in ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Guns = ServerStorage:WaitForChild("Guns")
local CloneTool = ReplicatedStorage:WaitForChild("CloneTool")

CloneTool.OnServerEvent:Connect(function(plr, toolName)
    local char = plr.Character
    
    if not plr:FindFirstChild(toolName, true) and not char:FindFirstChild(toolName) then
        -- I don't think you'd want users to have multiple guns. If you do, just get rid of this 
        local gun = Guns[toolName]:Clone()
        gun.Parent = plr.Backpack
        gun:Clone().Parent = plr.StarterGear
    end
end)

###Also, it "worked" in studio because you used Play Solo, and Play Solo is the server and client combined. There is no separation. This is why you were able to access ServerStorage from a local script. They will most likely be separated in the future. For now, don't use Play Solo to test your scripts. Use test/local servers to do this.

2 comments

0

06:51:30.861 - ServerScriptService.Script:12: attempt to index global 'player' (a nil value)

0

Sorry lol

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