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

gun not goning in players backpack mouse1click??

Asked by
hokyboy 270 Moderation Voter
5 years ago
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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
06:51:30.861 - ServerScriptService.Script:12: attempt to index global 'player' (a nil value) hokyboy 270 — 5y
0
Sorry lol User#19524 175 — 5y
Ad

Answer this question