Passing player via touch event?

  1. What do you want to achieve?
    I’d like to be able to pass the player to the remote event fire call
  2. What is the issue?
    I cannot pass the player as an argument in the event fire call.
  3. What solutions have you tried so far?
    I’ve tried using:
local human = player:GetPlayerFromCharacter(base.Parent)
	print(human.Name)

Code for brick that is touched (NORMAL SCRIPT)

local part = script.Parent
local remoteEvent = game.ReplicatedStorage.loader1
local player = game:GetService("Players")
part.Touched:Connect(function(base)

	local human = player:GetPlayerFromCharacter(base.Parent)
	print(human.Name)
	--remoteEvent:FireClient(human)

	
	
end)

Code for GUI event/onFireEvent event (LOCAL SCRIPT)

local remoteEvent = game.ReplicatedStorage.loader1
local frame = script.Parent
local tweenService = game:GetService("TweenService")

remoteEvent.OnClientEvent:Connect(function()
	local targetPos = UDim2.new(0.158, 0, 0.784, 0)
	local info = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
	local tween = tweenService:Create(
		frame,
		info,
		{Position = targetPos}
	)
end)

I get the error that “human” isn’t a player but the code says otherwise, I’m quite confused and have no clue what to do.

2 Likes

Edit: I think the problem is that the Touched is detecting the character model instead of its children. You might wanna do some sanity checks…

3 Likes

when you fire a remote event from the client, it automatically receives a player argument.

2 Likes

Try this:

part.Touched:Connect(function(base)
        if base.Parent:FindFirstChild("Humanoid") then
        local human = game.Players:FindFirstChild(base.Parent.Name)
    end
end)
1 Like

Yes i mean at the touched event where hes getting the player from the character

1 Like

maybe I interpreted it wrong… but I don’t think it will detect the players character instead of the players arm leg etc

1 Like

Yea might need some checks.

local part = script.Parent
local remoteEvent = game.ReplicatedStorage.loader1
local player = game:GetService("Players")
part.Touched:Connect(function(base)

	local human = player:GetPlayerFromCharacter(base.Parent)

	if human then -- human should be nil if GetPlayerFrom....() doesn't obtain a Player so code proceeds
		print(human.Name)
		--remoteEvent:FireClient(human)
	end


	
	
end)
1 Like

add a check to see if it is a player or not
such as

local part = script.Parent
local remoteEvent = game.ReplicatedStorage.loader1
local players = game:GetService("Players")

part.Touched:Connect(function(otherPart)
  local char = otherPart.Parent
  local humanoid = char:FindFirstChildOfClass("Humanoid")
  if char and humanoid then
    local player = players:GetPlayerFromCharacter(char)
    if player then
      print(player.Name)
      --whatever else you wanna do
    end
  end
end)
1 Like

there is a possibility that it is touching other parts which why it returns errors

1 Like
local part = script.Parent
local remoteEvent = game.ReplicatedStorage.loader1
local player = game:GetService("Players")
part.Touched:Connect(function(base)

	local human = base.Parent:FindFirstChild("Humanoid") or base.Parent.Parent:FindFirstChild("Humanoid"
if human then
	local player = player:GetPlayerFromCharacter(human.Parent)
	remoteEvent:FireClient(player)
end

	
	
end)