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

Why does localplayer fail to cast as a string?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
But it prints the local player name. I want the model holder name. So therefore that wouldnt really make much sense voidofdeathfire 148 — 5y
0
Use the second example that has two parameters and it should work. royaltoe 5144 — 5y
0
I edited the post so you can see what I'm talking about royaltoe 5144 — 5y
0
playerName is the parameter you put inside :FireServer() royaltoe 5144 — 5y
View all comments (2 more)
0
shouldve been accepted EmbeddedHorror 299 — 5y
0
shouldve been accepted EmbeddedHorror 299 — 5y
Ad

Answer this question