Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

why does my hand to gui not work (giving player tool from another player)?

code:

game.ReplicatedStorage.Events.HandTo.OnServerEvent:Connect(function(player)
	local co =	player.Backpack.Tool:Clone()
	local Playertogive = game.Players:FindFirstChild(player.PlayerGui.ScreenGui.TextBox.Text)
	co.Parent = Playertogive
	Playertogive.Tool.Parent = Playertogive.Backpack
	print("Moved!")
	player.Backpack.Tool:Destroy()
end)

error: ServerScriptService.Script:5: attempt to index nil with 'Tool'

2 answers

when a player types something in a TextBox, it doesn't replicate to the server

you need to send the text as an argument to the RemoteEvent

also, reading from or editing guis on the server in general is kind of extremely nasty

-- somewhere in your localscript...
game.ReplicatedStorage.Events.HandTo:FireServer(path.to.textbox.Text)

-- server script
game.ReplicatedStorage.Events.HandTo.OnServerEvent:Connect(function(player, text)
    local tool =  player.Backpack.Tool:Clone()
    local playerToGive = game.Players:FindFirstChild(text) -- inconsistent variable casing is Not Good
    if playerToGive then
        tool.Parent = playerToGive
        player.Backpack.Tool:Destroy()
    end
end)

If the player doesn't exist or if the user spelt it wrong it can break the script. Adding a simple if statement can help!

game.ReplicatedStorage.Events.HandTo.OnServerEvent:Connect(function(player)
local co =  player.Backpack.Tool:Clone()
local Playertogive = game.Players:FindFirstChild(player.PlayerGui.ScreenGui.TextBox.Text)
if Playertogive then
  co.Parent = Playertogive
  Playertogive.Tool.Parent = Playertogive.Backpack
  print("Moved!")
  player.Backpack.Tool:Destroy()
else
  print("Player Not Found")
end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.