Firing event from server script to local script doesn't work

I’m using a server script for my looting system. It fires an event to a local script. The local script turns off the player’s inventory when they are looting and disables the stamina script in startergui. I’ve tried IvenDisable:FireAllClients(). Nothing, no errors. I’ve tried IvenDisable:FireClient() and I get this error: Argument 1 missing or nil Help?

Server Script

local Detector = script.Parent.ClickDetector
local ItemsStorage = game.ReplicatedStorage.Items


local debounce = false

Detector.MouseClick:Connect(function(Player)
	local character = Player.Character
	local humanoid = character.Humanoid	
	local PickUp = humanoid:LoadAnimation(script.PickUpAnim)
	
	local IvenDisable = script.Parent.Events.InventoryDisable
	
	Player.PlayerGui.LootGUI.LootingText.Visible = true
	
	local randomnumber = math.random(1, 5)
	
	Player.Character.Humanoid.WalkSpeed = 0
	
	IvenDisable:FireClient()
	
	if debounce then return end
	debounce = true
	
	wait(5)
	Player.PlayerGui.LootGUI.LootingText.Visible = false	
	
	if randomnumber == 5 then 
	PickUp:Play()
	local Clone = ItemsStorage.Clothing.DBlueJeans:Clone()
    Clone.Parent = Player.Character
	end
	
	if randomnumber == 4 then 
	PickUp:Play()
	local Clone = ItemsStorage.Clothing.DBrownJeans:Clone()
	Clone.Parent = Player.Character
	end
	
	if randomnumber == 3 then 
	PickUp:Play()
	local Clone = ItemsStorage.Clothing.DWhiteTShirt:Clone()
	Clone.Parent = Player.Character
	end
	
	if randomnumber == 2 then 
	PickUp:Play()
	local Clone = ItemsStorage.Clothing.RedFlannelShirt:Clone()
	Clone.Parent = Player.Character
	end
	
	if randomnumber == 1 then 
	PickUp:Play()
	local Clone = ItemsStorage.Clothing.FishingHat:Clone()
	Clone.Parent = Player.Character
	end
	
	Player.Character.Humanoid.WalkSpeed = 13

	task.wait(1) -- 5 minute cooldown = 300.  set to 1 for testing purposes
	debounce = false
	
end)

Local Script:

local Player = game.Players.LocalPlayer -- general setup


local IvenDisable = script.Parent.Events:WaitForChild("InventoryDisable")


IvenDisable.OnClientEvent:connect(function()
local StarterGui = game:GetService("StarterGui")	
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) -- disable inventory	
end)
1 Like

First of all, it needs the player to fire there V

Also, you can use elseif in if statements. And you don’t have to play PickUp animation you can just do

PickUp:Play()
if randomNumber == 1 then
       --The code
elseif randomNumber == 2 then
       --The code
end

Edit V
Also, can you please tell me why you set a variable to humanoid there V

then used V

Also, you used if debounce then return end in the middle of the script!!! So this will cause bugs. As an example, you did set WalkSpeed to 0 before doing ``ìf debounce then return end``` so you will be stuck!!!

And if you use more variables it will make it more readable!!! :grin:
So I’m gonna edit your script a little bit.

local detector = script.Parent.ClickDetector
local clothingStorage = game.ReplicatedStorage.Items.Clothing


local debounce = false

detector.MouseClick:Connect(function(player)
	if debounce then return end
	debounce = true

	local lootingText = player:FindFirstChild("playerGui"):FindFirstChild("LootingGui"):FindFirstChild("LootingText")
	local char = player.Character
	local humanoid = char.Humanoid	
	local pickUp = humanoid:LoadAnimation(script.PickUpAnim)

	local InventoryDisableEvent = script.Parent.Events.InventoryDisable

	lootingText.Visible = true

	local randomNumber = math.random(1, 5)

	player.Character.Humanoid.WalkSpeed = 0

	InventoryDisableEvent:FireClient(player)

	wait(5)
	lootingText.Visible = false

	pickUp:Play()

	if randomNumber == 5 then 
		local clone = clothingStorage.DBlueJeans:clone()
		clone.Parent = player.Character
	elseif randomNumber == 4 then
		local clone = clothingStorage.DBrownJeans:clone()
		clone.Parent = player.Character
	elseif randomNumber == 3 then
		local clone = clothingStorage.DWhiteTShirt:clone()
		clone.Parent = player.Character
	elseif randomNumber == 2 then
		local clone = clothingStorage.RedFlannelShirt:clone()
		clone.Parent = player.Character
	elseif randomNumber == 1 then
		local clone = clothingStorage.FishingHat:clone()
		clone.Parent = player.Character
	end

	humanoid.WalkSpeed = 13

	task.wait(1) -- 5 minute cooldown = 300. Set to 1 for testing purposes
	debounce = false
end)

And I changed the names of some variables because your variable setting style is so complicated. Like you sometimes do local DoubleText = "double" and local doubletext = "double" and this will make it harder to remember variables in bigger scripts.

Thanks for reading! :slightly_smiling_face:

I’ve changed some of the your code and I noticed some errors. local lootingText = player:FindFirstChild("playerGui"):FindFirstChild("LootingGui"):FindFirstChild("LootingText") doesn’t seem to work, so I changed it back to Player.PlayerGui.LootGUI.LootingText.Visible = false The event code still doesn’t seem to work though. It’s almost like the script skips over it since I don’t get any errors in output.

Edit:
I’ve added print("Fired Event") on the server script and print("Received Event") for the local script. The server script seems to have fired it properly, but I don’t think the local script received it even though there are no errors and the code seems fine.

1 Like