Remote Event and Hit.Parent

In my case I have a script in a part which when stepped on fires a remote event. That remote event activates a local script which then does something. I was wondering if I can access hit.parent from the first script in the local script. Is there anyway to do this?

1 Like

Just like that

local Player = game:GetService("Players").LocalPlayer

local Part = -- path to the target part
Part.Touched:Connect(function(Hit)
	if Hit.Parent ~= Player.Character then		return		end
	
	-- Code
end)
2 Likes

I appreciate the response. This isn’t really what I meant thought, I’m sorry if what I said was confusing.

Currently I have a script in a part -

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent:RemoteEvent = ReplicatedStorage:WaitForChild("TimerStart")


local Debounces = {}

script.Parent.Touched:Connect(function(touched)
	if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(touched.Parent)
		if Player and not Debounces[Player] then
			Debounces[Player] = true
			remoteEvent:FireClient(Player)
		end
	end
end)

remoteEvent.OnServerEvent:Connect(function(plr)
	Debounces[plr] = nil
end)

Players.PlayerRemoving:Connect(function(plr)
	Debounces[plr] = nil
end)

Once that part is hit a remote event is fired. This remote event then fires a local script in StarterPlayerScripts -

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("TimerStart")

local function teleport()
-- access the hit.parent from first script here..
end

remoteEvent.OnClientEvent:Connect(teleport)

I was wondering if I could get the hit.parent in that local script from the first script.

Isn’t hit.Parent always in the local script going to be the player’s character?

You can do Player.Character in that case.

You can set other parameter in the remote event, and get this parameter in the local script. Like this:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent:RemoteEvent = ReplicatedStorage:WaitForChild("TimerStart")


local Debounces = {}

script.Parent.Touched:Connect(function(touched)
	if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(touched.Parent)
		if Player and not Debounces[Player] then
			Debounces[Player] = true
			remoteEvent:FireClient(Player, touched.Parent) --we set the hit parent like parameter
		end
	end
end)

remoteEvent.OnServerEvent:Connect(function(plr)
	Debounces[plr] = nil
end)

Players.PlayerRemoving:Connect(function(plr)
	Debounces[plr] = nil
end)

Do you already tried this?