Hello!
So I’m assuming this is a gui button that when pressed will put an item inside the user’s backpack? If so, this method you are using is not going to work because guis utilize local scripts which only work on the client.
→ See here to learn more: Understanding the Difference Between Server Side and Client Side
This means that re-parenting the object into the player’s backpack will only be seen by the player and the activation event tied to the object will only fire inside of local scripts stored inside the object (which in your case isn’t what you want because you’re using the ‘ClassicSword’ which uses Scripts opposed to Local Scripts that can only execute code on the server).
So to fix this issue we will firstly utilize RemoteEvents. A remote event is an object that the client can reference and call upon in order to relay information the server
–>> See more here: Remote Events and Callbacks | Documentation - Roblox Creator Hub
In this case, we would want to use RemoteEvents to act as a middle-man in telling the server to clone the sword in ReplicatedStorage and put it into the Player’s backpack to ensure that the sword is given to the player on the Server rather than just the Client. We do this by creating a new RemoteEvent inside of ReplicatedStorage (I would rename it to something like “CharacterSelect” for organization purposes) and then reference it inside the local script of the button.
-- example
local REP = game:GetService('ReplicatedStorage')
local CharacterSelect = REP:WaitForChild('CharacterSelect')
local Button = script.Parent -- referencing the button
Button.MouseButton1Click:Connect(function()
-- some code ...
CharacterSelect:FireServer('Insert Character Name Here')
end)
Once you reference it within a variable, you would then create a regular script inside of ServerScriptService in order for the server to detect when the RemoteEvent is fired.
Here’s an example of what that may look like:
local REP = game:GetService('ReplicatedStorage')
local CharacterSelect = REP:WaitForChild('CharacterSelect')
local Sword = REP:WaitForChild('ClassicSword')
-- in remote events, the first argument of the function
-- is always the player, the second is whatever the client
-- passes through it. So in this case, we passed the string
-- 'Insert Character Name Here'
CharacterSelect.OnServerEvent:Connect(function(Player, Character)
if Character == 'Insert Character Name Here' then
-- the ":Clone()" method duplicates an
-- instance its called upon. This is favorable
-- in this case because we don't want to just re-parent
-- the sword inside the player's backpack because that
-- we need the original sword to clone for constant use.
local Backpack = Player.Backpack
local SwordClone = Sword:Clone()
SwordClone.Parent = Backpack
-- p.s. if you still want to kill the player,
-- I'd advise you still do it on the server.
end
end)
Then you’re pretty much done. If you have any questions, feel free to ask!