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

Why does localplayer fail to cast as a string?

I'm wondering why you can't send this through a remote event Also in case your wondering I am firing from a sword and want to get the sword holder's name

Keeps erroring and giving me Unable to cast value to Object

local name = game.Players.LocalPlayer.Character.Name
print(name)
event:FireClient(name)

1 answer

When you fire a remote event to the server, the first parameter is the player and the rest of the parameters are the parameters inside :FireServer(these are the rest of your parameters)

This is the code that you did

playerName is the player object that you see in game.Players in the explorer, not your variable that you passed to the server. That would be the second parameter.

The game gets confused since you can't convert a player object to a string.

createPartEvent.OnServerEvent:Connect(function(playerName)
	print(playerName)
end)

You need to put the player as the first parameter or else the rest of your parameters won't be correct.

#EDIT: USE THIS EXAMPLE VVVVV The code below is correct since it has the player object as the first parameter followed by any other variables you passed when firing the event:

createPartEvent.OnServerEvent:Connect(function(player, playerName)
	--the variable you sent:
	print(playerName)
end)

You could also do this since you can access the player's name from the player object,

createPartEvent.OnServerEvent:Connect(function(player)
	print(player.Name)
end)

6 comments

0

But it prints the local player name. I want the model holder name. So therefore that wouldnt really make much sense

0

Use the second example that has two parameters and it should work.

0

I edited the post so you can see what I'm talking about

0

playerName is the parameter you put inside :FireServer()

0

shouldve been accepted

0

shouldve been accepted

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