How to fire the event with onTouched function?

Hello everyone! I am trying to write a script that, when a part is touched, will fire the event. Here is what I wrote

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimpleEvent = ReplicatedStorage.SimpleEvent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')

function onTouched(hit)
	if hit.Parent.Humanoid then
		print('Working')
        SimpleEvent:FireServer()
	end
end
script.Parent.Touched:connect(onTouched)

Your code is firing an event, I don’t understand what your having issues with?

Hmmm, huh? Just print (‘Working’) for some reason is not written, I do not understand this.

Change this to
if hit.Parent:FindFirstChild("Humanoid") then

Still don’t understand what your saying, don’t rush your sentences when typing and use fluent english.

Sorry I am using a translator. I added a script to the ServerScriptService that should do the following after firing the event, but it doesn’t.
Here is a script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimpleEvent = ReplicatedStorage.SimpleEvent

SimpleEvent.OnServerEvent:Connect(function(player)
	print('RemoteEventHasPicked')
	game.Workspace.Simplepart.Anchored = false
end)

I’m newer in scripting, but you can follow the instructions below. I tried it and it worked.

  1. Put this script into StarterPlayer > StarterPlayerScripts > LocalScript

LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimpleEvent = ReplicatedStorage.SimpleEvent
local Player = game.Players.LocalPlayer
function onTouched(hit)
    local PlayerCheck = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) -- Gets player from the part, because player's character hit the part.
    if PlayerCheck then -- Checks if the player hit the part
        print('Working')
        SimpleEvent:FireServer()
    end
end

game.Workspace.Simplepart.Touched:connect(onTouched)

  1. Put this script into ServerScriptService

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimpleEvent = ReplicatedStorage.SimpleEvent
SimpleEvent.OnServerEvent:Connect(function(player)
    print('RemoteEventHasPicked')
    game.Workspace.Simplepart.Anchored = false
end)
  1. Check your Explorer menu, it must look like this;

ExplorerMenu

  1. Here is the video, it is working;

If you are experiencing any issues with that, please let me know.

I hope this is helpful to you.

1 Like

Thanks

(30 symbols

You’re welcome :slight_smile:

(30 symbols