Hey, I have problem with this script, the main function of this script is to add to a player accessory (Wings) from ServerStorage after player will click a gui button. Accessory should attach to a player UpperTorso but for some reason my script doesn't work. So how can i fix that script to attach this Accessory to player (Accessory should be visible to all players on the server not only for the client)
Any help is going to be appreciated :)
local A = game.Players.LocalPlayer.Character local humanoid = A:WaitForChild("Humanoid") script.Parent.MouseButton1Click:Connect(function() if humanoid then humanoid:AddAccessory(game.ServerStorage.Wings:Clone()) local handle = game.ServerStorage.Wings.Handle local attachmentH = Instance.new("Attachment", handle) attachmentH.Position = A.humanoid.UpperTorso end end)
Console error: "Wings is not a valid member of ServerStorage" (Wings accessory is placed in ServerStorage)
The Server is the only thing that can access ServerScriptService
and the ServerStorage
. The client cannot access these items which further secures objects inside those services.
The reason Wings
comes back nil
is because the client cannot see objects inside the ServerStorage
.
We need to use a RemoteEvent
for the client to be able to communicate with the server. Once the client presses the GUI button, we can use the RemoteEvent
to :FireServer()
, basically sending a signal to the server to do something.
local humanoid = Character.Humanoid Button.MouseButton1Click:Connect(function() local arg1 = humanoid local arg2 = 'Wings' RemoteEvent:FireServer(arg1, arg2) end)
In order to make this work the server has to listen for signals that clients are giving them. OnServerEvent
will listen for every time a LocalScript
runs :FireServer()
on a RemoteEvent
. From there we can clone and attach things and it'll replicate on everyone's screen.
RemoteEvent.OnServerEvent:Connect(function(player, arg1, arg2) local playerWho_FireServer = player local humanoid = arg1 local HatRequest = arg2 local Hat = ServerStorage:FindFirstChild(HatRequest) if (Hat) then --clone, parent to char, Humanoid:AddAccessory() end end)
Now I named these variables to make sure you understand what is going on. We are sending specific arguments the the Server so the Server knows what Humanoid
needs what Hat
. What we did here is the client sent its HumanoidObject
as well as a String
telling which hat to look for in the ServerStorage
. The Server then looks for the hat and checks if it exists. Pretty sure you can move on from there.
Good luck!