Help with RemoveAccessories Remote Event

So this is my server script that tells the remote event what to do(it works)

game.ReplicatedStorage.HatDestroy.OnServerEvent:Connect(function(player)
	local char = player.Character
	if char then
		for i,v in pairs(char:GetChildren()) do
			if v.Name == "Humanoid" then
				v:RemoveAccessories()
			end
		end
		for i,v in pairs(char:GetChildren()) do
			if v.Name == "Head" then
				v.face.Transparency = 1
			else
				return
			end
		end
	end
end)

This is my original script in StarterPlayer > StaterCharacterScripts that I made for testing, and when I use “game.Workspace.Lava” it works

db = false
game.Workspace.Lava.Touched:Connect(function()
	if db == false then
	db = true
	game.ReplicatedStorage.HatDestroy:FireServer()
	end
	wait(5)
	db = false
end)

I’m now trying to move the LocalScript from StarterCharacterScripts into a part using the script.Parent.Touched event but it isn’t working.

db = false
script.Parent.Touched:Connect(function()
	if db == false then
	db = true
	game.ReplicatedStorage.HatDestroy:FireServer()
	end
	wait(5)
	db = false
end)

I’ve just recently gotten back into coding and I’m not very good yet, so I’m sure there is something I’m doing wrong here, but I would appreciate being pointed in the right direction if anyone knows whats going on.

1 Like

Try using

script.Parent.Touched:connect (onTouched)

That isn’t the issue.

This works

game.Workspace.Lava.Touched:Connect(function()

This doesn’t work

script.Parent.Touched:Connect(function()

It has something to do with remote events I think

Just to confirm, have you tried printing in the new Touched statement to see if it is detecting the touch or just not firing the event?

Just tried, and yeah it’s not printing for the new Touched statement that uses script.Parent, seems my debounce isnt working on the original one either because it’s spamming prints

First of all just gonna fix some code here for you are there are a few things wrong with the original.

db = false
script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		if db == false then
		    db = true
		    game.ReplicatedStorage.HatDestroy:FireServer()
	        wait(5)
		    db = false
		end
	end
end)
1 Like

The script that is detecting the touch is a LocalScript. If you’re saying it wont even print anything when touched I would consider looking to see if the script is even running. LocalScripts do not run when parented to workspace.

Then how would I go about doing this without having the localscript in the part in the workspace?

Put it inside the StarterPack. You already have a reference to the script, it shouldn’t be an issue that way.

1 Like

There is no need to have it in workspace. Is there a specific reason as to why you want the script itself to be in workspace?

yes, so for example my game this is going into is a tycoon. I’m adding this script into one of the Morph givers. I cant figure out any other way to determine which part is to be touched (in the original scripts its game.Workspace.Lava) but in the tycoons its like game.Workspace.TycoonKit.Tycoon1.Purchases and there’s 4 different tycoons. I was using script.Parent to determine what part the player actually touches to fire the remote event because originally the localscript is in StarterPlayerScripts and says game.Workspace.Lava.Touched:Connect(function()

Why not use a ServerScript instead of a LocalScript?

script.Parent.Touched:Connect(function(Hit)
    if not Hit.Parent:FindFirstChild("Humanoid") then return end;
	local char = Hit.Parent
	if char then
		for i,v in pairs(char:GetChildren()) do
			if v.Name == "Humanoid" then
				v:RemoveAccessories()
			end
		end
		for i,v in pairs(char:GetChildren()) do
			if v.Name == "Head" then
				v.face.Transparency = 1
			else
				return
			end
		end
	end
end)
1 Like

I thought for Remove Events that you have to fire the event from the client through a local script. Then the server script tells the remove event what to do.

Yes but there is no need for a RemoteEvent. You can remove the Hats and detect the Touched from a server script. See my previous response for an example.

Edit: Ill provide better code for you, give me a moment.

1 Like

I guess I was overthinking it smh, yep it works no need for better code. I appreciate it.

script.Parent.Touched:Connect(function(Hit)
	local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid then
		Humanoid:RemoveAccessories()
		
		if Humanoid.Parent.Head.face then
			Humanoid.Parent.Head.face:Destroy()
		end
	end
end)
1 Like

Awesome, oh yeah and i found out today that it is “face”, rather than “Face” lol

1 Like

Just to let you know Are31 Remote events Can be fired from client and server for Example from server you say RemoteEvent:FireAllClients() But for Local Scripts you use RemoteEvent:FireServer() I hope this help you and taught you a little bit

1 Like

That’s interesting. I’ll keep it in mind. I also have to learn remote functions too.

@Are31 1 I am going to DM check your dms soon @lolhi0404