Cloning a model from storage to a player using Remote Events

Gday!
I’m still getting used to the Remote Events and I wanted to try by myself to have a GUI button that, once pressed, a sword gets copied from ServerStorage and parented to the player.

Placement:
image

LocalScript:

local storage = game.ReplicatedStorage
local button = script.Parent
local cam = game.Workspace.CurrentCamera
local ClassSelection = script.Parent.Parent.Parent
local PlayButton = script.Parent.Parent.Parent.Parent.PlayButtonFrame


button.MouseButton1Click:Connect(function()
	storage.PickWeapon:FireServer()
	local camblur = cam.Blur
	camblur.Size = 0
	cam.CameraType = "Custom"
	cam.FieldOfView = 80
	ClassSelection.Visible = false
	PlayButton.Visible = false
end)

Give Weapon:

local ReplicatedStorage = game.ReplicatedStorage
local ServerStorage = game:GetService("ServerStorage")
local Greatsword = ServerStorage.Greatsword

ReplicatedStorage.PickWeapon.OnServerEvent:Connect(function(player)
	local GreatswordCopy = Greatsword:Clone()
	GreatswordCopy.Parent = player
end)

There are no errors in the output which is why im confused as to why this is not working. Any help would be appreciated. Thank you!

Optionally, I’d like to know if and how one could copy multiple things from ServerStorage to the player that clicks on the GUI button. For an example, an animation to play with the sword, some damage values that might be edited later, bunch of other animations for different factors in the game.

1 Like

The give weapon server Script is parenting the sword incorrectly. It should be like this:

local ReplicatedStorage = game.ReplicatedStorage
local ServerStorage = game:GetService("ServerStorage")
local Greatsword = ServerStorage.Greatsword

ReplicatedStorage.PickWeapon.OnServerEvent:Connect(function(player)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		local GreatswordCopy = Greatsword:Clone()
		GreatswordCopy.Parent = backpack
	end
end)

This script will parent it to player’s backpack also known as inventory or hotbar. If you want the player to have it equipped when parenting the sword then you can do GreatswordCopy.Parent = player.Character

The sword will be equipped right away and won’t have the classic roblox toolbar and such. Pretty much the weapon will be on the player at all time, but in two states: Idle and Combat. Can the backpack be edited in such ways…or?

Just parent it to the character then.

local ReplicatedStorage = game.ReplicatedStorage
local ServerStorage = game:GetService("ServerStorage")
local Greatsword = ServerStorage.Greatsword

ReplicatedStorage.PickWeapon.OnServerEvent:Connect(function(player)
	local character = player.Character
	if character then
		local GreatswordCopy = Greatsword:Clone()
		GreatswordCopy.Parent = character
	end
end)

Then you can code custom functionality for the idle and combat states.

Unless I’m a dummy, the sword does not appear in the player’s model. Or is it supposed to be this way?

It’s supposed to be like this…
Just try it, it should work.

I added a print part at the ServerEvent. It works but the sword model still isn’t visible inside the player’s model.

The player’s model custom so Player.Character hasn’t been set.
How are you creating the “player’s model”?
Can I see the portion of script that’s doing that.

At the beginning of this topic I have listed the 2 scripts that are used for the sword, remote event, cloning and whatnot. Other scripts are only for the camera animation, nothing related to the player’s character. I saw on the Dev Forum that if player is mentioned inside function(player), the script will happen to the local player that, in my case, clicks the GUI button. Therefore I didn’t find the need to actually define nor create a player’s model.

Try debugging it like this:

ReplicatedStorage.PickWeapon.OnServerEvent:Connect(function(player)
	local character = player.Character
    print(character)
    print(Greatsword)
	if character then
		local GreatswordCopy = Greatsword:Clone()
		GreatswordCopy.Parent = character
	end
end)

It prints my username and the sword’s name.

Greatsword has to be Tool.
It also has to be welded properly if there are more parts than just Handle.

I’ve added it as a Tool and it still doesn’t work. I’ve also tried this link but none of them work.

Edit: I’ll try to use the weapon as a tool but hide the backpack UI as in this forum post.