FireClient won't work in my situation

Hello!
–Introduction–
I’m making a game like slap battles but instead of slapping, it’s punching.
I’m making a glove called “Country Glove”, which pops up an image of a country flag every time you get hit by it.
–The problem–
I try to make the country flag disable using USA.Enabled = false on a server script, and I realized that you cannot do that on a server script, so I used :FireClient and .OnClientEvent and it seems to not work!

–Server Script–

local DebounceTable = {}

script.Parent:WaitForChild("Handle").Touched:Connect(function(objectThatTouchesTheHitbox)
	if objectThatTouchesTheHitbox.Parent then
		if objectThatTouchesTheHitbox.Parent:FindFirstChild("Humanoid") then
			if DebounceTable[objectThatTouchesTheHitbox.Parent] == true then return end
			DebounceTable[objectThatTouchesTheHitbox.Parent] = true
		    local player = script:FindFirstAncestorWhichIsA"Player" or game:GetService"Players":GetPlayerFromCharacter(script.Parent.Parent)
			local PunchKB = require(game.ServerScriptService.PunchKBCountry)
			PunchKB:knockBack(player.Character, objectThatTouchesTheHitbox.Parent)
			player.leaderstats.Brawls.Value += 1
			script.Parent.VineBoomSoundEffect:Play()
			local randomNumber = math.random(1,6)
			if randomNumber == 1 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.USA.Enabled = true
				wait(2)
				game.ReplicatedStorage.USAReplicate:FireClient(pEnemy)
			end
			if randomNumber == 2 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Canada.Enabled = true
				wait(2)
				game.ReplicatedStorage.CanadaReplicate:FireClient(pEnemy)
			end
			if randomNumber == 3 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Russia.Enabled = true
				wait(2)
				game.ReplicatedStorage.RussiaReplicate:FireClient(pEnemy)
			end
			if randomNumber == 4 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Ukraine.Enabled = true
				wait(2)
				game.ReplicatedStorage.UkraineReplicate:FireClient(pEnemy)
			end
			if randomNumber == 5 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Brazil.Enabled = true
				wait(2)
				game.ReplicatedStorage.BrazilReplicate:FireClient(pEnemy)
			end
			if randomNumber == 6 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Mexico.Enabled = true
				wait(2)
				game.ReplicatedStorage.MexicoReplicate:FireClient(pEnemy)
			end
			wait(1)
			DebounceTable[objectThatTouchesTheHitbox.Parent] = nil
		end
	end
end)
local randomNumber = math.random(1,6)
			if randomNumber == 1 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.USA.Enabled = true
				wait(2)
				game.ReplicatedStorage.USAReplicate:FireClient(pEnemy)
			end
			if randomNumber == 2 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Canada.Enabled = true
				wait(2)
				game.ReplicatedStorage.CanadaReplicate:FireClient(pEnemy)
			end
			if randomNumber == 3 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Russia.Enabled = true
				wait(2)
				game.ReplicatedStorage.RussiaReplicate:FireClient(pEnemy)
			end
			if randomNumber == 4 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Ukraine.Enabled = true
				wait(2)
				game.ReplicatedStorage.UkraineReplicate:FireClient(pEnemy)
			end
			if randomNumber == 5 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Brazil.Enabled = true
				wait(2)
				game.ReplicatedStorage.BrazilReplicate:FireClient(pEnemy)
			end
			if randomNumber == 6 then
				local enemy = objectThatTouchesTheHitbox.Parent
				local pEnemy = game.Players:GetPlayerFromCharacter(enemy)
				pEnemy.PlayerGui.Mexico.Enabled = true
				wait(2)
				game.ReplicatedStorage.MexicoReplicate:FireClient(pEnemy)
			end

This is the code that I was struggling with.
–Client Script for USA Flag–

game.ReplicatedStorage.USAReplicate.OnClientEvent:Connect(function(pEnemy)
	pEnemy.PlayerGui.USA.Enabled = false
end)

How do I fix this?

1 Like

Can u explain some more, Like whats the error ur getting??

2 Likes

So, what I am understanding ( please correct me if wrong) , you want to disable the screengui.

There is no error at all, but it doesn’t work.

Yes. I want to disable it after 2 seconds.

I would like to point out you are doing the ‘randomnumber’ if statements in a way where the script is waiting , idk if thats what you want whenever it does a if statement , but if not use task.wait

is task.wait() a new version of wait()?

also if you want you can do an table or array for ex, so you don’t need all of those if statements

local countries = {[1] = "USA"; [2]   =  "Canada"; [3] = "Russia"; [4] = "Ukraine";       }
local ChosenCountry = table.find(countries, randomNumber) -- and now you have your random country

Sorry I did not feel like putting all the countries in , but this is a better way, someone correct this script if incorrect

1 Like

task.wait() does not make the whole script wait only the function

it does not work. also is there anything wrong with my client script?

local randomNumber = math.random(1,6)
			local countries = {[1] = "USA"; [2]   =  "Canada"; [3] = "Russia"; [4] = "Ukraine";   [5] = "Mexico";  [6] = "Brazil";    }
			local ChosenCountry = table.find(countries, randomNumber) -- and now you have your random country
			print(ChosenCountry)

When I print it out, it prints out nil

It does work for the purpose I gave you, just not with the script , I only pointed out a few more “obvious” things that could be improved on,

IF pEnemy is a player and that is being launched to the player (pEnemy) then it look okay but has one thing, you dont need pEnemy or the player object in the onclientevent, just put a local pEnemy= game.Players.localplayer
also if the event on the country is decently simple you can only use one remote event and just put to the fireClient(PEnemy, ChosenCountry)

So remove the pEnemy from that line

Hello, you don’t need table.find , I tested out a little bit,

local randomNumber = math.random(1,6)
			local countries = {[1] = "USA"; [2]   =  "Canada"; [3] = "Russia"; [4] = "Ukraine";   [5] = "Mexico";  [6] = "Brazil";    }
			local ChosenCountry =  countries[randomNumber] -- and now you have your random country
			print(ChosenCountry)

I recommend not doing anything related to the GUI on the server as this is bad practice.

local remote = -- your's RemoteEvent.

local function disable_frame(frame)
	frame.Enabled = false
end

local function replicate(player_gui, name)	
	local frame = player_gui[tostring(name)]
	
	task.delay(2, (disable_frame), frame)	
	frame.Enabled = true
end

remote.OnClientEvent:Connect(replicate)

I am so stupid but found a solution for this problem that has affected me about 1/2 of this day.

I used :GetPropertyChangedSignal as the local script of each of my guis so that I wouldn’t have to wait on the server.

Code:

script.Parent:GetPropertyChangedSignal("Enabled"):Connect(function()
	if script.Parent.Enabled == true then
		wait(2)
		script.Parent.Enabled = false
	end
end)

Thank you to everyone who was there to try to help me!