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

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

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?

11 comments

0

You don't have to put the define local player for argument 1 while firing the event

0

If that's not the case then can you provide the whole code for it?

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.

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.

0

Please put in the client-side code where the FireServer function is ran.

0

Okay!

0

Done. I edited the post.

0

Simple error just says it wants a string instead of a instance. a string looks like this "Lol" or 'Lol'

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

0

I think it's because it passed the line

0

hmmmm i know what a string is i just dont know how to pass one in a remote event.

2 answers

✓ Accepted answer

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.

1 comment

0

Thank you. It worked!

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

3 comments

0

Thank you too! Can I accept both answers?

0

You can't accept both answers. You can only accept one answer.

0

@kidsteve923, why is the first letter of every word in capitals?

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