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

Unable to cast value to object?

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

✓ Accepted answer

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)

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!

3 comments

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

0

yeah, but if I use plr1 it still says that its an object

0

in both game.Players:FindFirstChild(pl) and game.Workspace:FindFirstChild(pl)

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