Parenting a tool to a players character help!

Event.OnServerEvent:Connect(function(player, item, animation, frame)
		--print(player, item, animation, frame.Name)
		local Tool = item:Clone() -- Clones the item
		Tool.Parent = game.Workspace.player -- Gives it to player
		Tool.Name = "EquippedWeapon"
		local char = game.Workspace.player
		char.Animate.toolnone.ToolNoneAnimation.Id = "http://www.roblox.com/asset/?id="..animation
		frame.Parent = frame.Parent.Parent
		frame:TweenPosition(UDim2.new(-1.015, 0, 0.771, 0),"Out","Quart",0.1) -- Side of the screen
end)

This is what I currently have set up for giving an item to a player but for some reason it doesn’t want to parent and I get an error saying that player is not a valid member of workspace. Not sure how to do this, I did add a characteradded event and that didn’t seem to work, so I am not sure what to do. Player is the name of the player which gets sent from the client to the server.

3 Likes

Try this instead:

Tool.Parent = player.Character

What you were trying to do was parent the tool to something in the workspace called player

2 Likes

Also, change

local char = game.Workspace.player

to this:

local char = player.Character
1 Like

Alright, I will try this.

30

I still get an error saying Index nil with Parent.

Also I did print(player) and It printed my name, so that why I did game.Workspace.player because player passed along the event was my player.

Its doing that because there is nothing called player in workspace.

Edit: You could do this maybe:

local Player = game.Workspace:FindFirstChild(player.Name)

I have tried this, and it doesn’t work.

What is the error?

30 charrrrrrrrrs

Same error, attempt to index nil with Parent

Try using this:

Event.OnServerEvent:Connect(function(player, item, animation, frame)
    local char = player.Character or player.CharacterAdded:Wait()
    local tool = item:Clone()
    tool.Name = "EquippedWeapon"
    tool.Parent = char
    -- rest of your code. Use "char" to reference the character, and "player" to reference the player
end

I think it works, but I get an error saying, attempt to index nil with Name so the tool.Name line.

This means that you are not sending over an “item” when you fire the RemoteEvent

This is what I currently have for it:

script.Parent.MouseButton1Click:Connect(function()
	local item2 = script.Parent.Parent.ItemView:GetChildren()
	for i, v in pairs(item2) do
		if v:IsA("Tool") then
			for _, c in pairs(v:GetChildren()) do
				if c:IsA("Script") then 
					local tool = v
					local animation = c:WaitForChild("Animation"):WaitForChild("Id").Value
					local frame = script.Parent.Parent
					local char = player.Character
					Equip_Event:FireServer(char, tool, animation, frame)
				end
			end
		end
	end
end)

It’s really messy.

I also tried local tool = item2

Here’s your problem:

When you fire the RemoteEvent this way, your replacing “item” in the other script with “char”

Dont worry about sending over “char” in the first parameter. Just send:

Equip_Event:FireServer(tool, animation, frame)

When the ServerScript receives this request, it automatically sets a player value:

Event.OnServerEvent:Connect(function(player, item, animation, frame)
2 Likes

Thank you this does seem to work. I really appreciate the help.

No problem! Glad I could help :slight_smile: