✓ Accepted answer
I hope this helps. It will replace some of your references so it waits for the child (yields until the child exists).
Note: If you're using this in a LocalScript, you can't access ServerStorage from the LocalScript !!!
You may want to use a RemoteEvent for it. If you are doing this for test purposes (Not recommended as the client can get the tools without any problem), you can try ReplicatedStorage, a container where both server and client replicate with.
local me = game.Players.iiiAmericanLight
local chr = me.Character or me.CharacterAdded:Wait()
local swr = game:GetService("ServerStorage"):WaitForChild("Meliodas Sword")
local pfr = game:GetService("ServerStorage"):WaitForChild("PurgatoryFlame")
local sw = swr:Clone()
local pf = pfr:Clone()
sw.Parent = me.Backpack
pf.Parent = me.Backpack
But, I'm sure you want it for practical purposes. Let's setup first your local side script.
I'll explain the process below the codes.
Here's the Local Script
local event = game:GetService("ReplicatedStorage"):WaitForChild("ClientRequest")
local player = game.Players.LocalPlayer
if player.Name == "iiAmericanLight" then
event:FireServer()
end
Here's the server script
local event = Instance.new("RemoteEvent")
event.Parent = game:GetService("ReplicatedStorage")
event.Name = "ClientRequest"
event.OnServerEvent:Connect(function(player)
local swr = game:GetService("ServerStorage"):WaitForChild("Meliodas Sword")
local pfr = game:GetService("ServerStorage"):WaitForChild("PurgatoryFlame")
local sw = swr:Clone()
local pf = pfr:Clone()
sw.Parent = player.Backpack
pf.Parent = player.Backpack
end)
What this actually does is, when the LocalScript runs (Supposedly you're using this LocalScript inside StarterGui), it assigns the player variable to the local player. then, through the if statement, it checks if the player's name is equal to the name you supposedly want to give such feature. and if it is, it then fires to the server through the remote connection we set up. Then in the ServerScript, it receives the interaction and since all client connections to the server return the player who effectuated the connection, we can use this for our server-side code. Now we can access ServerStorage and do our stuff! I've also removed your character variable as it wasn't needed but you can add it in your server-side script. The final stage is that the script gets the tools from the serverstorage, clones it and locates them in your Backpack. This also works with FilteringEnabled, as the work of each side belongs to their respective sides, that's it: server-sided tasks (accessing ServerStorage and distributing objects) must be in the server script, and local-side tasks (processing the request, setting up the stuff), must be in the local script! I hope it helped.
4 comments
If you give me the error message I can help.
There is no error message, works perfectly but only in studio
Okay, so this means you'll have to fire an event scripting the sw and pf into 'me's' inventory. If this doesn't work, it's most likely because your game is FE.
When you make scripts they work as soon as the server starts running. In studio it waits for the client. To avoid things not loading in just add a wait function for 5 or 10 seconds at the start of each script. It really works.