So… I was making a shooting game where you can select weapons and stuff to deploy, the select weapon value worked (which I use string value) as the value will change to the selected weapon’s text.
The value folder
The Weapons Folder
I got an error which said that Guns is not a valid member of ServerStorage "ServerStorage"
Heres the script…
local deploy = script.Parent.Deploy
local player = game.Players.LocalPlayer
local gun1Value = game.ReplicatedStorage.gun.Value
local gun2Value = game.ReplicatedStorage.secondaryGun.Value
local gun2 = game.ServerStorage.Guns:FindFirstChild(gun2Value)
local gun1 = game.ServerStorage.Guns:FindFirstChild(gun1Value)
deploy.MouseButton1Click:Connect(function()
gun1:Clone()
gun1:Clone().Parent = player.Backpack
end)
The children and descendants of ServerStorage arent accessible from LocalScripts. If you are giving the players tools, it should be done in a serverscript anyway otherwise the tool won’t function properly.
if ‘Deploy’ is a textbutton, i believe that click events also fire in the server if a player clicks it, so you shouldnt need to use a remote event. you would just handle the click event in the server
Clone the gun from a Server script, and parent it into the player’s backpack.
You could use a RemoteEvent to ask the server to give it to you, but ultimately, you still have to clone it on the server.
You have to clone the gun and put in in the player’s backpack from the Server, because the Guns Folder is in ServerStorage, and you can access it only from server scripts.
I made a script in ServerScriptService and heres the result…
local script in the gui
local deploy = script.Parent.Deploy
local gun1Value = game.ReplicatedStorage.gun.Value
local gun2Value = game.ReplicatedStorage.secondaryGun.Value
deploy.MouseButton1Click:Connect(function()
local Fire = game.ReplicatedStorage.selectWeapon
Fire:FireServer(gun1Value)
end)
serversciptservice script
game.ReplicatedStorage.selectWeapon.OnServerEvent:Connect(function(plr, gun1Value)
for i,v in pairs(game.ReplicatedStorage:FindFirstChild(gun1Value):GetChildren()) do
for c,d in pairs(game.ServerStorage.Guns:GetChildren()) do
if v.Name == d.Name then
print(d.Name)
local Clone = d:Clone()
Clone.Parent = plr.Backpack
end
end
end
end)
it returned an error which says ServerScriptService.Script:2: attempt to index nil with "GetChildren"
what does that mean? I am not really good at events and stuff… ;-;
You are trying to find a child of replicatedstorage with the same name as the value of their gun1value, which doesnt exist, so you cant use getchildren on it.
Is there a reason you dont just find gun1value in the serverstorage and then clone it rather than using for loops?
I dont know how to include code in posts so ill use blockquotes. It would look something like this, but I don’t know what your game hierarchy looks like so i cant assure its accuracy
local serverstorage = game:GetService(“ServerStorage”)
local guns = serverstorage:WaitForChild(“Guns”)
game.ReplicatedStorage.selectWeapon.OnServerEvent:Connect(function(plr, gun1value)
local gun = guns:FindFirstChild(gun1value)
if gun then
gun = gun:Clone()
gun.Parent = plr.Backpack
end