hey, im currently trying to make an event fire once a part is touched, but its not working, and its not giving me errors aswell. this is my script:
script.Parent.Touched:Connect(function(hit) -- The part has been touched
if hit.Parent:FindFirstChild("Humanoid") then -- The touched's parent has a humanoid in it
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player by the character (the hit.Parent is our player in this case)
if player then -- If the player is here
game.ReplicatedStorage["ClearAccEvent"]:FireClient(player) -- Put in the event name your remote event's name, this fires the event.
end
end
end)
i also added print statements, but it said it was working when clearly its not:
script.Parent.Touched:Connect(function(hit)
print("Part touched!") -- Debug message to check if the touch event is firing
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
print("Humanoid found!") -- Debug message to check if the humanoid is found
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
print("Player found:", player.Name) -- Debug message to check if the player is found
game.ReplicatedStorage.ClearAccEvent:FireClient(player)
print("Event fired to player:", player.Name) -- Debug message to check if the event is fired
else
print("Player not found for humanoid:", humanoid.Parent.Name) -- Debug message if player is not found
end
else
print("Humanoid not found in touched part:", hit.Parent.Name) -- Debug message if humanoid is not found
end
end)
Your code is correct, but the reason nothing is happening is that (by pure assumption) you haven’t set up a LocalScript that listens for when the remote event is fired. In order to receive the event from the server to the client, you use OnClientEvent.
game.ReplicatedStorage.ClearAccEvent.OnClientEvent:Connect(function()
-- do whatever "ClearAcc" means here
end)
Please note that while you fire that event (through a Script) you must pass the player as an argument (which you already have done). However when you receive the event (through a LocalScript), you don’t need to define the player, as you can define them as game.Players.LocalPlayer, because it’s a LocalScript.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClearAccEvent = ReplicatedStorage.ClearAccEvent
-- CLEAR HATS
ClearAccEvent.OnServerEvent:Connect(function(player)
local originalFace
player.CharacterAdded:Connect(function(char)
local hd = char:FindFirstChildOfClass("Humanoid"):FindFirstChildOfClass("HumanoidDescription")
originalFace = hd.Face
-- print(originalFace)
end)
local playerchar = player.Character
local Humanoid = playerchar:FindFirstChildOfClass("Humanoid")
if not Humanoid then
return
end
local HumanoidDescription = Humanoid:FindFirstChild("HumanoidDescription")
if not HumanoidDescription then
return
end
local face = HumanoidDescription.Face
local X = playerchar:GetChildren()
local ww = 0
for i, v in pairs(X) do
if v:IsA("Accessory") then
-- print("Removed", v.Name, "for", player.Name)
v:Destroy()
ww = ww + 1
end
end
HumanoidDescription.BackAccessory = ""
HumanoidDescription.FaceAccessory = ""
HumanoidDescription.FrontAccessory = ""
HumanoidDescription.HairAccessory = ""
HumanoidDescription.HatAccessory = ""
HumanoidDescription.NeckAccessory = ""
HumanoidDescription.ShouldersAccessory = ""
HumanoidDescription.WaistAccessory = ""
HumanoidDescription.Face = face
Humanoid:ApplyDescription(HumanoidDescription)
ClearAccEvent:FireClient(player, ww)
end)
Change OnServerEvent:Connect(function(player) to OnClientEvent:Connect(function(). You’re looking to communicate from the server to the client. Moreover, there’s no need to look for the player argument, as it is passed to OnClientEvent by default. Define the player (as I stated in my previous reply) before setting up the connection to the event.
No. The script where you use FireClient is a script in the part. The script where you use OnClientEvent should be a LocalScript, which should be located in StarterPlayerScripts.
ah i see, basically i had the script which uses the OnServerEvent in the serverscriptservice because gui wise i had a button that would fire that would fire it, so i just tried reusing that from the part. so should i just make a seperate local script in the starterplayerscript that uses onclientevent
Yes. You would use OnServerEvent if communication occurred from the client towards the server. Since you want to communicate from the server towards the client, you should use OnClientEvent. Feel free to read the official documentation for more info on RemoteEvents and BindableEvents.
If I were to describe BindableEvents, they’re kind of like RemoteEvents, meaning that they are also able to pass information, except that information can be passed between only one type of script.
You might want to use BindableEvents here, in case you want the effect of removed accessories to be visible to other players. Or, you know, you might as well just get rid of the idea of using any types of events and simply handle accessory deletion through the Touched event as it is, since you already define the player in there.