Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

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)
Ad
Log in to vote
-1
Answered by 4 years ago

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)

Answer this question