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

Unable to cast value to object?

Asked by 6 years ago

I'm trying to get the text from a textbox, and tp all invited players to a private server But when I try to get the player, and I get this error: Unable to cast value to object

--Local script in a textbutton
script.Parent.MouseButton1Click:Connect(function()
    local pl = script.Parent.Parent.TextBox1.Text
    local plr = game.Players:GetPlayerFromCharacter(pl)
    game.ReplicatedStorage.ownerp4:FireServer(plr)
end)
--Script in workspace
game.ReplicatedStorage.ownerp4.OnServerEvent:Connect(function(n, plr)
print(plr)
local TS = game:GetService("TeleportService")
code = TS:ReserveServer(2097256710)
TS:TeleportToPlaceInstance(2097256710, code,{plr})
end)

I get my name in the console, but I don't get teleported to the server

2 answers

Log in to vote
0
Answered by
Ghusoc 50
6 years ago

The GetPlayerFromCharacter() function requires the actual character model, but if you look at pl - it's a variable referencing the Text inside of TextBox1, so a string.

You could instead use FindFirstChild() using pl: `local plr = game.Players:FindFirstChild(pl)

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Okay, so I changed it to:

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.Parent.Parent.global.Value == 1 then
    local pl = "SharkOfGod"
    local plr1 = game.Workspace:FindFirstChild(pl)
    local plr = game.Players:GetPlayerFromCharacter(plr1)
    print(plr1)
    print(plr)
    game.ReplicatedStorage.ownerp4:FireServer(plr1)
end
end)

But plr is still an object, and if I use

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.Parent.Parent.global.Value == 1 then
    local pl = "SharkOfGod"
    local plr1 = game.Players:FindFirstChild(pl)
    local plr = game.Players:GetPlayerFromCharacter(plr1)
    print(plr1)
    print(plr)
    game.ReplicatedStorage.ownerp4:FireServer(plr1)
end
end)

"plr" is nil EDIT: So I used plr1 and changed the server script a bit, and it worked!

0
no, you misunderstand, Players:GetPlayerFromCharacter gets the PLAYER instance from the CHARACTER model, so something like game.Players:GetPlayerFromCharacter(game.Players.foo.Character) would return game.Players.foo CPF2 406 — 6y
0
yeah, but if I use plr1 it still says that its an object User#23477 0 — 6y
0
in both game.Players:FindFirstChild(pl) and game.Workspace:FindFirstChild(pl) User#23477 0 — 6y

Answer this question