Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Remote event is not firing?

I'm trying to make a script that removes all accessories when the player joins the game and works with Filtering enabled. I am using remote events but they are not firing.

-- This is in a local script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local RemoveAccessoriesEvent = ReplicatedStorage.Events:WaitForChild("RemoveAccessoriesEvent")

RemoveAccessoriesEvent:FireServer()

-- This is in a server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoveAccessoriesEvent = ReplicatedStorage.Events.RemoveAccessoriesEvent


local function onRemoveAccessoriesFired(player)
	warn("RemoveAccessoriesEvent has been fired") --// Not showing which means the event is not being fired
	local char = player.Character
	while true do
		wait(0.1)
		for i,v in pairs(char:GetChildren())do
			if v.ClassName == "Accessory"and v.Name~="Blonde"and v.Name~="Cyan"and v.Name~="Green"and v.Name~="MidnightBlue"and v.Name~="Normal"and v.Name~="Pastel"and v.Name~="PatrioticShaggy"and v.Name~="Purple"and v.Name~="Rainbow"and v.Name~="Recycled"and v.Name~="Red"and v.Name~="Shaggy2"and v.Name~="Tix"and v.Name~="auburn"and v.Name~="neonpink" then
				v:Destroy() 
			end
		end
	end	
end

RemoveAccessoriesEvent.OnServerEvent:Connect(onRemoveAccessoriesFired)

3 comments

0

Is the LocalScript in StarterPlayerScripts and the Script in ServerScriptService? If either are in ReplicatedStorage, they will not run. Are either of the scripts Disabled? Put a print before and after you call FireServer on the LocalScript, to confirm it fires. Do you have any other error messaages in the Output?

0

So I get an error saying attempt to index local 'char' (a nil value)

0

This means that at the time the RemoteEvent is fired, the Character has not loaded. local char = player.CharacterAdded:Wait()

2 answers

✓ Accepted answer

You don’t need a remote.

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        for _, v in pairs(char:GetChildren()) do
            if v:IsA"Accoutrement" then -- if it inherits from this class (e.g. accessory)
                v:Destroy() 
            end
        end
    end)
end)

You shouldn't need a RemoteEvent for this situation- you can simply use the CharacterAppearanceLoaded event of a player as soon as someone spawns. This event fires as soon as the player's appearance finished loading, meaning all hats should be there and can quickly be removed.

-- Server Script
game.Players.PlayerAdded:Connect(function(player) -- Player joined the game
	player.CharacterAppearanceLoaded:Connect(function(char) -- fires when their appearance has loaded fully after respawning.
		for i,v in pairs(char:GetChildren()) do
			if v:IsA("Accoutrement") then -- All hats/accessories inherit from Accoutrement. Put other logic here.
				v:Destroy()
			end
		end
	end)
end)

4 comments

0

Not :IsA accessory. But :IsA accoutrement. That code wouldn’t destroy anything since IsA finds objects that INHERITS from accessory. I don’t think accessory has inherited classes.

0

@incapaz what? That's not how IsA works. It would also destroy Accessories. I'll go ahead and change it but you should read the wiki lol. http://robloxdev.com/api-reference/function/Instance/IsA

0

misunderstood first, but the wiki also says this, which should be taken into consideration if you don’t want inheriting classes being destroyed:

0

Returns true if an Instance is a member of the given class, or if the Instance's class inherits from the given class.

This discussion is preserved as it was. New answers, comments, and votes are closed.