Bro what is this

Hello, i have a problem. When you click on the mouse button when a coconut is equipped, half a coconut should appear in inventory, All these things are in the serverStorage. help me please

This local script in coconut:

local player = game:GetService(“Players”).LocalPlayer
local coconut = player.Backpack:FindFirstChild(“Coconut”)
local halfCoconutModel = game:GetService(“ServerStorage”).Tools.HalfCoconut:Clone()
halfCoconutModel.Parent = player.Backpack

local function onButton1Down()
if coconut then
halfCoconutModel:Clone().Parent = player.Character
end
end

game:GetService(“UserInputService”).InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
onButton1Down()
end
end)

Screenshot_3

image

image

From looking at this image I can tell its a client script. Client scripts cannot access anything related to the server. For example it can’t access ServerStorage and ServerScriptService. Only server-sided scripts can access those this could be why you are possibly getting that error message.

1 Like

Bro i fix this but i have new error, Players.MaaXGaz20.Backpack.Coconut.Script:2: attempt to index nil with ‘Backpack’

Did you move it to a server script? game:GetService(“Players”).LocalPlayer doesn’t work in a server script, so player will be nil.

But since the script is in the backpack, you could set the player like this:

local player = script.Parent.Parent

He can use this:

game.Players.PlayerAdded:Connect(function(Player)
       -- getting player in Server
end)

This would not be a good way to do it my opinion.
Since the script is in the backpack, the PlayerAdded caused by this player joining would most likely run before the script itself runs.
Also, this would every time another player joins, but we really only care about the one player in this case.

I’m not really sure if this is a good idea, but how about this one?

for _,player in pairs(game.Players:GetPlayers()) do
     --code
end

you probably typed in
game.ServerStorage.ServerStorage.Tools.

The client can’t access the ServerStorage.
Instead, try to store it in ReplicatedStorage, it’s just ServerStorage but public.

2 Likes

ServerScriptService and ServerStorage do not Replicate their Descendants to the Client. You can read the Documentation for it, which openly states that they do not Replicate anything to the Client.


Both ServerScriptService and ServerStorage are very Important Services for you, They allow you to store very important Assets of your game like Models, or Scripts into a Secure location that only the Server can Access, and not the Client.

The Client is basically what the Player see’s on their Screen and what is Replicated to them, Almost everything is Replicated to the Client, with the exception of the said Services, Since the other stuff replicates to the Client, they can easily be taken or Manipulated by Exploiters, while the Server is something that Exploiters cannot easily access duting runtime, which makes it important for Handling stuff.

If you intend to use stuff for both Client and Server, use the Service: ReplicatedStorage
ReplicatedStorage is basically stuff shared between the Server and Client, but keep in mind that you shouldn’t trust this Service with very important things as it can be accessed by Exploiters.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.