Not giving players tools with no errors?

I am making it, so that when the player is a spy or a soldier, they get a tool. But they aren’t getting the tool with no error.
Local script:

local remoteEvent = game.ReplicatedStorage:WaitForChild("GUIEvent")
local WeaponEvent = game.ReplicatedStorage:WaitForChild("WeaponEvent")
local Player = game.Players.LocalPlayer
remoteEvent.OnClientEvent:Connect(function(selectedRole)
	script.Parent.Visible = true
	if selectedRole == "Scientist" then
    	script.Parent.TextSize = 50 
    	script.Parent.Text = "Scientist, stick with soldiers!"
    	wait(4)
    	script.Parent.Parent.Visible = false
	elseif selectedRole == "Soldier" then
		WeaponEvent:FireServer(Player,selectedRole)
    	script.Parent.TextSize = 50
    	script.Parent.Text = "Soldier, protect scientists against the spy!"
    	wait(4)
    	script.Parent.Parent.Visible = false
	elseif selectedRole == "Spy" then
		WeaponEvent:FireServer(Player,selectedRole)
    	script.Parent.TextSize = 50
    	script.Parent.Text = "Spy, kill soldiers and scientists, and gather data!"
    	wait(4)
    	script.Parent.Visible = false
	end
end)

Script:

local WeaponEvent = game.ReplicatedStorage:WaitForChild("WeaponEvent")
WeaponEvent.OnServerEvent:Connect(function(Player, selectedRole)
	if selectedRole == "Soldier" then
		local tool = game.ReplicatedStorage.Pistol:Clone()
	    tool.Parent = Player.Backpack
    	script.Parent.Parent.Visible = false
	elseif selectedRole == "Spy" then
		local tool2 = game.ReplicatedStorage.ClassicSword:Clone()
	    tool2.Parent = Player.Backpack
	end
end)

Are you sure that your Event is firing?

Use prints to see if code runs and if it doesnt then, try to print out your Variables.

1 Like

the event is firing because it is printing, but it won’t check the roles

Did you print your roles?

If they are nil, they wont work.

1 Like

Nvm, i guess it is correct, i will still take a look at the devhub.

1 Like

It prints the players name when I try to print the role

Im pretty sure they are firing from a Local Script.

1 Like

I am getting confused, do I fire it from a script?

Ah I see, the issue is this:
When firing an event from a LocalScript to the server, your first variable will always be the player.

Your Event is:

Event:FireServer(PLAYER, Variable)

But it should be:

WeaponEvent:FireServer(selectedRole)

Because Player Will be there by default. :wink:

2 Likes

I made an edit, Be sure to change it to Event:FireServer(selectedRole)

1 Like

I got the error " [11:16:36.359 - Players.SadWowow21.PlayerGui.ClassGui.Frame.TextLabel.LocalScript:4: attempt to index function with 'Connect"

1 Like

SelectedRole is a value? Like SelectedRole.Value make sure of that

1 Like

In firing a server you don’t need to add player as a parameter because when you connect a RemoteEvent that is fired by the client to a function the first argument is already the player even though you didn’t set it as a parameter when you fired the server.

In this code:

WeaponEvent:FireServer(Player,selectedRole)

it would pass 3 parameters. (The main player, an argument that isn’t supposed to exist, and the selected role)

instead do this:

WeaponEvent:FireServer(selectedRole)

in this line of code it would only pass 2 parameters which is the main player and the selected role.

1 Like

Separate note but I highly recommend divorcing these systems. Exploiters can just fire your remotes and receive all the tools given that there’s no security and you’re letting the client be authoritative of deciding what role it wants to be. Not only that, but considering the content of the script, I can already tell you have a regular script and a RemoteEvent in a Gui which is improper.

There should be no remotes involved at any step here. Your current workflow incurs unnecessary network use in sending data over and lack of security on the remote. Instead, the server should be responsible for handling tool distribution based on how roles are chosen. It should then fire a RemoteEvent in ReplicatedStorage so for the client, only to display what role the server selected for them. It’s already clear that you’ll know what role a player has from the server given the code.

4 Likes