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

invalid argument #2 (string expected got instance) How to fix?

Asked by 2 years ago
Edited 2 years ago
game.ReplicatedStorage.equipSword.OnServerEvent:Connect(function(player, sword)
    player.equippedSword.Value = sword
end)

client/gui code

script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.equipSword:FireServer(game.Players.LocalPlayer, "Biograft")
end)

When I fire the event in the gui while playing in studio this error pops up in the output,

invalid argument #2 (string expected got instance)

The sword is a string and it's supposed to change the player's equipped sword value to Biograft sword so that in the round they spawn with that sword. Any ideas on how to fix it?

0
You don't have to put the define local player for argument 1 while firing the event acediamondn123 147 — 2y
0
If that's not the case then can you provide the whole code for it? acediamondn123 147 — 2y
0
This is pretty much all the code for it. Or at least the one I think isn't working. I'm pretty sure that if you remove the player part it will fire for everybody in the server if I'm correct. Oliver_Bocch 32 — 2y
0
Plus if I remove the player argument what player will be requesting to equip it. That would just do it for all players, which is not what I want. Oliver_Bocch 32 — 2y
View all comments (7 more)
0
Please put in the client-side code where the FireServer function is ran. satyajit_ray 60 — 2y
0
Okay! Oliver_Bocch 32 — 2y
0
Done. I edited the post. Oliver_Bocch 32 — 2y
0
Simple error just says it wants a string instead of a instance. a string looks like this "Lol" or 'Lol' MarkedTomato 810 — 2y
0
Also where does the error happen? it will tell you what line the error occurred in the output or simply just click the error and it will show you the line MarkedTomato 810 — 2y
0
I think it's because it passed the line RektwayYTB 123 — 2y
0
hmmmm i know what a string is i just dont know how to pass one in a remote event. Oliver_Bocch 32 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

This should help

So you're trying to when a player clicks a Gui object it fires a RemoteEvent to the server and changes the equippedSword value on the server so the value will be visible to the server, not just the client.

LocalScript:

script.Parent.MouseButton1Click:Connect(function() -- when GUI object clicked
    game.ReplicatedStorage.equipSword:FireServer("Biograft") -- fires to server passing a string value
end)

ServerScript:

game.ReplicatedStorage.equipSword.OnServerEvent:Connect(function(client, sword) -- by default the `Client` parameter is always there by default
    player.equippedSword.Value = sword -- changes the equippedSword string value to "Biograft"
end)

What you did before was weird because when doing the :FireServer() function you don't need to give the player just the arguments you want to pass to the server.

More information on RemoteEvents

RemoteEvents

FireServer

OnServerEvent

Any questions just reply below and explain what trying to do exactly.

0
Thank you. It worked! Oliver_Bocch 32 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

This Is Just Because Your Sending A Player Instance If You Dont Know When Sending Remote Events The Recieving Script Automatically Gets The Player Instance To Fix It Just Do This

script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.equipSword:FireServer("Biograft")
end)

Still Didnt Answered Your Problem Well Do This Then

game.ReplicatedStorage.equipSword.OnServerEvent:Connect(function(player, UniqueNameForString)
    player.equippedSword.Value = UniqueNameForString
end)

The Problem Might Be Because You Might Have Another Instance Called "sword" And The Script Thinks That Your Talking About The Instance While Your Talking About The String Hope This Fixes It :D

0
Thank you too! Can I accept both answers? Oliver_Bocch 32 — 2y
0
You can't accept both answers. You can only accept one answer. JesseSong 3916 — 2y
0
@kidsteve923, why is the first letter of every word in capitals? JesseSong 3916 — 2y

Answer this question