Why won't my RemoteEvent code work?

Basically, I want that button to fire a RemoteEvent which has 10 cmds. The localscript will be able to fire a cmd depending on the conditions.
Localscript:

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Full.Value = false
script.Parent.Parent.Image = “rbxasset://textures/ui/GuiImagePlaceholder.png”
if script.Parent.Parent.Item.Value == “” then
print(“nothing in slot 1”)
elseif script.Parent.Parent.Item.Value == “Hematogen 1” then
script.Parent.Parent.Parent.Hematogens.Value = script.Parent.Parent.Parent.Hematogens.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 30
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“1”)
elseif script.Parent.Parent.Item.Value == “Hematogen 2” then
script.Parent.Parent.Parent.Hematogens.Value = script.Parent.Parent.Parent.Hematogens.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 30
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“2”)
elseif script.Parent.Parent.Item.Value == “Medkit 1” then
script.Parent.Parent.Parent.Medkits.Value = script.Parent.Parent.Parent.Medkits.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 100
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“3”)
elseif script.Parent.Parent.Item.Value == “Medkit 2” then
script.Parent.Parent.Parent.Medkits.Value = script.Parent.Parent.Parent.Medkits.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 100
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“4”)
elseif script.Parent.Parent.Item.Value == “Medkit 3” then
script.Parent.Parent.Parent.Medkits.Value = script.Parent.Parent.Parent.Medkits.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 100
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“5”)
elseif script.Parent.Parent.Item.Value == “Bandage 1” then
script.Parent.Parent.Parent.Bandages.Value = script.Parent.Parent.Parent.Bandages.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 50
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“6”)
elseif script.Parent.Parent.Item.Value == “Bandage 2” then
script.Parent.Parent.Parent.Bandages.Value = script.Parent.Parent.Parent.Bandages.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 50
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“7”)
elseif script.Parent.Parent.Item.Value == “Bandage 3” then
script.Parent.Parent.Parent.Bandages.Value = script.Parent.Parent.Parent.Bandages.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 50
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“8”)
elseif script.Parent.Parent.Item.Value == “Bandage 4” then
script.Parent.Parent.Parent.Bandages.Value = script.Parent.Parent.Parent.Bandages.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 50
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“9”)
elseif script.Parent.Parent.Item.Value == “Bandage 5” then
script.Parent.Parent.Parent.Bandages.Value = script.Parent.Parent.Parent.Bandages.Value - 1
script.Parent.Parent.Parent.TotalPriceValue.Value = script.Parent.Parent.Parent.TotalPriceValue.Value - 50
script.Parent.Parent.Item.Value = “”
game.Workspace.PharmacyItemScript.PharmacyItem:FireServer(“10”)
end
end)

The localscript works, but for some reason the listener server script won’t.

script.PharmacyItem.OnServerEvent:Connect(function(cmd)
if cmd == “1” then
game.Workspace.PharmacySystem.Items.Hematogen1.Taken.Value = false
game.Workspace.PharmacySystem.Items.Hematogen1.Transparency = 0
elseif cmd == “2” then
game.Workspace.PharmacySystem.Items.Hematogen2.Taken.Value = false
game.Workspace.PharmacySystem.Items.Hematogen2.Transparency = 0
elseif cmd == “3” then
game.Workspace.PharmacySystem.Items.Med_Kit1.Taken.Value = false
game.Workspace.PharmacySystem.Items.Med_Kit1.Transparency = 0
elseif cmd == “4” then
game.Workspace.PharmacySystem.Items.Med_Kit2.Taken.Value = false
game.Workspace.PharmacySystem.Items.Med_Kit2.Transparency = 0
elseif cmd == “5” then
game.Workspace.PharmacySystem.Items.Med_Kit3.Taken.Value = false
game.Workspace.PharmacySystem.Items.Med_Kit3.Transparency = 0
elseif cmd == “6” then
game.Workspace.PharmacySystem.Items.Bandage1.Taken.Value = false
game.Workspace.PharmacySystem.Items.Bandage1.Transparency = 0
elseif cmd == “7” then
game.Workspace.PharmacySystem.Items.Bandage2.Taken.Value = false
game.Workspace.PharmacySystem.Items.Bandage2.Transparency = 0
elseif cmd == “8” then
game.Workspace.PharmacySystem.Items.Bandage3.Taken.Value = false
game.Workspace.PharmacySystem.Items.Bandage3.Transparency = 0
elseif cmd == “9” then
game.Workspace.PharmacySystem.Items.Bandage4.Taken.Value = false
game.Workspace.PharmacySystem.Items.Bandage4.Transparency = 0
elseif cmd == “10” then
game.Workspace.PharmacySystem.Items.Bandage5.Taken.Value = false
game.Workspace.PharmacySystem.Items.Bandage5.Transparency = 0
end
end)

2 Likes

Your code’s too long, but I think your problem is that you’re using strings on numbers, in that case convert the number to a string for example :
In the ServerScript, handling what happens after the event fires.
to check whether it’s a string at all

script.PharmacyItem.OnServerEvent:Connect(function(cmd)
       if typeof(cmd)~="string"
       then print("cmd is not a string whereas you are using it as one")
    return --if it's not a string, you return 
end

The necessary code you would add would have to be under

script.PharmacyItem.OnServerEvent:Connect(function(cmd)
  local cmdx = tostring(cmd)--convert the number valuie to a string

assign the tostring(cmd) to a different variable and use that instead of cmd below.

Basically here is your error, when you use cmd, you are taking the number value and comparing it to a string , you should convert it to a string using tostring(cmd) first, and then do so.

However you could just remove the " quotation marks " from your numbers for instance where you wrote :

elseif cmd == “1” then

and do this instead

elseif cmd == 1 then

Would it work if I had replaced the numbers with words, like “one”, “two” and so on?

“one” and “two” are not recognized by roblox as numbers, they are strings, using tostring on 1 would print 1 in the output, just converted to a string to be compared to values in string , like those in " "

The only purpose I use those strings is just so I can have 1 remoteevent without having to have 10 of them. So, the fix is for me to remove the quotation marks from my numbers?

I mean do you get any errors in the ouput? May I ask why don’t you close each if statement and why do you close all of them with one ‘end’? Also I think you might be overusing the remote events.

No, I don’t get any errors in the output. If I did then I’d fix them.

And “overusing remoteevents”, you’re right! My game has 26 in ReplicatedStorage. Atleast it works except for here

Yeah it might work but this will decrease your game performance, just to mention this out. Overusing the remote events can cause a high ping as well. You can only have one remote event to do your thing you don’t need 26 of them, just send the value to server side and check the value with if statements.

your code is not very incorrect - though inefficient, to clarify what I mentioned before
remove the quotation marks so the value the parameter is compared to is not a string , as numbers and strings are separate things.

Once you receive a value from the client :

script.PharmacyItem.OnServerEvent:Connect(function(cmd)

You are comparing cmd to a string, because you do this

if cmd == "1"--here 1 is a string NOT a number

Doing this should solve the problem :

script.PharmacyItem.OnServerEvent:Connect(function(cmd)
  if cmd == 1--no "" marks

Do I also remove the quotation marks from the FireServer?

I don’t understand. Does that value have to do something with the fact that this doesn’t work? And yes it is a stringvalue.
All I want you to do is just to tell me how can I the listener that doesn’t work.

Where do you parent the RemoteEvent?

inside a script called PharmacyItemScript

You have to tell me where is this script located.

it is in Workspace.
My post must be atleast 30 characters so this last sentence is pointless

Can you try to give RemoteEvent inside ReplicatedStorage, Script in ServerScriptService and LocalScript leave it like it is. And make sure to re-write the scripts because you changed the parent of things.

The first parameter of an OnServerEvent is always the Player that fired it.

Remote.OnServerEvent:Connect(function(Player, Cmd)
     if Cmd == "whatever" then

     end
end)

Okay, so this is funny… When you do OnServerEvent, the first argument is the player who fired it. So to fix this you would add to your OnServerEvent:Connect() OnServerEvent:Connect(function(ply,cmd)) It should work now.

Oh lol I just realised that he didn’t even called it in the arguments.