Server Script doesn't work when trying to parent a tool to replicated storage

alright so i’ve been trying to make a fps game in which you can choose a loadout and change it. my idea for changing the loadout is just parenting the tools a player has in his backpack to replicatedstorage, but it just doesn’t work. here’s the script and where it is

local AK = game.StarterPack:FindFirstChild("AK101")
script.Parent.MouseButton1Click:Connect(function()
	game.StarterGui.GunChoose.GunChooseFrame.Visible = true
	if AK then
		AK.Parent = game.ReplicatedStorage
	end
end)

obraz

obraz
the “button” textbutton is the button that was suppost to parent every tool to the replicatedstorage

2 Likes

I don’t use the default Roblox icon pack but are you parenting a script to a text a button on the client?

uhhhhhhhhhhhhhhhhhhhh. im kind of new to scripting so i don’t exactly know what ur saying lol.

Modifying game.StarterGui does not immediately replicate to a client it only does once their character resets. To modify a players current ui you have to edit PlayerObject.PlayerGui instead of game.StarterGui

alright ill try that. (i hate the 30 text thingy)

is it suppost to be like this???

PlayerObject.PlayerGui.GunChoose.GunChooseFrame.Visible = true

Im assuming the script is running on the client so this

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
Player.PlayerGui.GunChoose.GunChooseFrame.Visible = true

Alright so basically it works, but not at all, it make’s the gui visible but still doesn’t parent the gun to replicatedstorage

what do i do then? (sry if im annoying, im just really bad at scripting)

Its the same issue as before. game.StarterPack isnt actually what is in the players backpack currently its just a template for when the character loads. Instead use Player.Backpack

Alright one more thing. so if i do Player.Backpack, do i also do “FindFirstChild” because im planning on adding more guns. so the player’s wont always have that exact gun.

Have a table filled with all the possible guns and loop through the table checking if the player has the corresponding gun which then you parent it to replicated storage

something like this? (im probablly wrong)

while wait(1) do
local Player = game.Players.LocalPlayer
local plr = game:GetService("Players")
local gun1 = Player.Backpack:FindFirstChild("AK101")
local gun2 = Player.Backpack:FindFirstChild("GLOCK17")
local guns = {gun1, gun2}
	if Player.PlayerGui.GunChoose.GunChooseFrame.Visible == true then
		guns.Parent = game.ReplicatedStorage
	end
end

script.Parent.MouseButton1Click:Connect(function(player)
    local AK = player.Backpack:FindFirstChild("AK101")
	player.PlayerGui.GunChoose.GunChooseFrame.Visible = true
	if AK then
		AK.Parent = game.ReplicatedStorage
	end
end)

if you want more guns just make a button for every gun

you can have a mouseButton1Click for every button

I went through this sort of issue before.

What I would suggest (This is also how I got through it) is having the local script fire a Remote Event and having a Server Script handle what that Event does (Cloning the tool to the player’s backpack)

could you somehow send me the script? i kind of don’t know how to do it.

I should mention too that instead of having the tools in ReplicatedStorage they should be in ServerStorage. You can’t make local scripts reference objects in ServerStorage as only the Server can see them which is why making the Event and Server Script is necessary.

I’ll try to give you a template. I’m currently not on my PC right now so I can’t do any testing

okay. i hope it works loollllll

script has to be in serverstorage/workspace/serverscript service

move it to one of those and then just reroute the variables and stuff.