How do you check if a text sent from a RemoteEvent is the same as one in the module

  1. What do you want to achieve?
    i want the main script to check if the text is the same as the one in the module script after the player has clicked a TextButton
  2. What is the issue?
    I’ve genuinely have got 0 clue how to
  3. What solutions have you tried so far?
    I’ve tried many, but I’m not sure how to say them

Module script:

local Actions = {
    ["Run"] = {
        ["Animation"] = "rbxassetid://3716636630",
        ["Price"] = 5,
        ["RespondingText"] = "Run Action"
    },
    
    ["Jump"] = {
        ["Animation"] = "rbxassetid://3716636630",
        ["Price"] = 5,
        ["RespondingText"] = "Jump Action"
    },
    ["Sit"] = {
        ["Animation"] = "rbxassetid://3716636630",
        ["Price"] = 5,
        ["RespondingText"] = "Sit Action"
    },
};

return Actions

main script (deleted all the bs i wrote here)


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
local Module = require(game.ServerScriptService.ActionHandler.Information)


ActionEvent.OnServerEvent:Connect(function(player, text)

end)

Local script (where it’s fired from)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
script.Parent.MouseButton1Down:Connect(function(text)
    ActionEvent:FireServer("Jump")
end)

Here is a picture of how everything is organized
image

do note that I don’t want to make a replicatedEvent for every action, since I will most likely have a lot more than 3 actions

It would work something like this

--MODULE
local module = {
	["Yeah"] = {
		["HeheTest"] = "mmmmhh bleh"
	},
}

return module
-- SERVER
game:GetService("ReplicatedStorage").Network.Test.OnServerEvent:Connect(function(player,text)
	print(Setter[text].HeheTest)
end)

image
As for your case, you would need to use this.

ActionEvent.OnServerEvent:Connect(function(player, text)
      print(Module[text].Animation)
end)

When I run this, it errors

19:38:28.278  ServerScriptService.ActionHandler:7: attempt to index nil with 'Animation'  -  Server - ActionHandler:7

is this because you can’t print it or is there another issue?
(tried it with Price aswell, since I assumed that you can’t print the animation ID, but it didn’t work either.)

It should definitely print. Can you try my example? I did what you posted in your thread but I used UIS instead. So it should work.

when I tried the script you provided originally it threw me out an error

Network is not a valid member of ReplicatedStorage "ReplicatedStorage"  -  Server - ActionHandler:12

your provided version
main

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
local Module = require(game.ServerScriptService.ActionHandler.Information)
game:GetService("ReplicatedStorage").Network.Test.OnServerEvent:Connect(function(player,text)
    print(Module[text].HeheTest)
end)

module


local module = {
    ["Yeah"] = {
        ["HeheTest"] = "mmmmhh bleh"
    },
}

return module

This is my script after doing the changed you provided me in the original post

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
local Module = require(game.ServerScriptService.ActionHandler.Information)


ActionEvent.OnServerEvent:Connect(function(player, text)
    print(Module[text].Animation)
end)

How is text passed again? Just a string?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
script.Parent.MouseButton1Down:Connect(function(text)
    ActionEvent:FireServer("Jump")
end)

text is passed trough the local script in a textbutton
image

ActionEvent.OnServerEvent:Connect(function(player, text)
    print(text)
    print(Module[text].RespondingText)
end)

try this debug.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
local Actions = require(game.ServerScriptService.ActionHandler.Information)

ActionEvent.OnServerEvent:Connect(function(player, text)
   if Actions[text] == nil then 
      print("Doesn't exist in actions module")
   else
      print("Action is valid...")
      print(Actions[text].RespondingText)
   end
end)

now it depends on what you are trying to achieve but if something

20:01:49.808  Jump  -  Server - ActionHandler:13
 20:01:49.808  ServerScriptService.ActionHandler:14: attempt to index nil with 'RespondingText'  -  Server - ActionHandler:14

anyways, @iFlameyz fixed the issue for me
I do want to thank you for helping me <3 I really appreciate it

You could either create a variable inside each one that is just the name itself and looop through each one to see if it’s name variable is the same

Module Script

local Actions = {
    ["Run"] = {
        ["Animation"] = "rbxassetid://3716636630",
        ["Price"] = 5,
        ["Name"] = "Run",
        ["RespondingText"] = "Run Action"
    },
    
    ["Jump"] = {
        ["Animation"] = "rbxassetid://3716636630",
        ["Price"] = 5,
        ["Name"] = "Jump",
        ["RespondingText"] = "Jump Action"
    },
    ["Sit"] = {
        ["Animation"] = "rbxassetid://3716636630",
        ["Price"] = 5,
        ["Name"] = "Sit",
        ["RespondingText"] = "Sit Action"
    },
};

return Actions

Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
local Module = require(game.ServerScriptService.ActionHandler.Information)

ActionEvent.OnServerEvent:Connect(function(player, text)
    for _, v in next, Module do
        if v.Name == text then
            -- your code here :)
        end
    end
end)

Or alternatively you could do

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ActionEvent = ReplicatedStorage.ActionEvent
local Module = require(game.ServerScriptService.ActionHandler.Information)

ActionEvent.OnServerEvent:Connect(function(player, text)
    for k, _ in next, Module do
        if k == text then
            -- your code here :)
        end
    end
end)

If you did the second one you wouldn’t need to modify the module script

but? that’s the same snippet as mine expect they added an if statement?

The script would error without the if check if the remote sent something else + trying to keep it as short and simple with prints to help sipo understand it or any person that might come across this post

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.