Help with inserting a name into a table on a touched event

Hey there,
I have this script here that is meant to insert a players name into the table when they touch a part. I’m only up to inserting a test word into the table which isn’t even working.
Help is greatly apricated

local sign = script.Parent
local Gplrs = {}
--table.insert(Gplrs)

sign.Touched:Connect(function(touching)
    print("1")
    table.insert(Gplrs, "Name")
    print("2")
    wait(5) 
    print(Gplrs[1])
 end
1 Like

I think you could just simply use the GetPlayerFromCharacter function, which basically detects if the part that got touched is equal to a Player’s Character Model so that you can insert the Name into:

local Sign = script.Parent
local Gplrs = {}

sign.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

    if Player and not table.find(Gplrs, Player.Name) then
        table.insert(Gplrs, Player.Name)
        wait(5)
        print(Gplrs[1])
    end
end)

Although do keep in mind that this would only print the first player that touched it, anyone else who touches it would keep printing that same first player

2 Likes

how would I make it print whoever was actually touching it?

You could just simply print out Player.Name if you wanted to print out who touched it once 5 seconds have passed, instead of printing out Gplrs[1]

1 Like

you can just do this

local Gplrs = {}

sign.Touched:Connect(function(touching)
    if touching and touching.Parent and touching.Parent:FindFirstChild("Humanoid") then -- checks if player
        local plr = touching.Parent:GetPlayerFromCharacter()
        Gplrs[#Gplrs+1] = plr.Name
    end
end)
1 Like

Im printing out Gplrs[1] because I just want to check that there name is actually going into the table.

Thank you, I will try it out!!

glad i could help!

This text will be hidden

Hey there, I tried out that script and nothing printed with no errors.

may i see your script?

This text will be hidden

Oh nvm Im getting an error now, Here it is: “GetPlayerFromCharacter is not a valid member of Model “Workspace.JackFr_ost” - Server - Script:6”
And here’s my script:

local sign = script.Parent.Parent
local Gplrs = {}

sign.Touched:Connect(function(touching)
	if touching and touching.Parent and touching.Parent:FindFirstChild("Humanoid") then -- checks if player
		local plr = touching.Parent:GetPlayerFromCharacter()
		Gplrs[#Gplrs+1] = plr.Name
	end
end)

Maybe because the player model and player uses the same name, you can just get the character name instead of the player?

can you try this

local sign = script.Parent.Parent
local Gplrs = {}

sign.Touched:Connect(function(touching)
	if touching and touching.Parent and touching.Parent:FindFirstChild("Humanoid") then -- checks if player
		local plr = touching.Parent
		Gplrs[#Gplrs+1] = plr.Name
	end
end)
1 Like

yep thank you, one second.
30ch

Now its not doing anything with no errors. does it need to be a local script?

no

This text will be hidden

hm idk why its not working then

You’re getting the error because the function GetPlayerFromCharacter is a function inside of the Player model, not the individual character;

local sign = script.Parent.Parent
local Gplrs = {}

sign.Touched:Connect(function(touching)
	if touching and touching.Parent and touching.Parent:FindFirstChild("Humanoid") then -- checks if player
		local plr = game:GetService("Players"):GetPlayerFromCharacter(t.Parent)
		Gplrs[#Gplrs+1] = plr.Name
	end
end)
2 Likes

oh, yes lmao. I forgot, this one here is the right one.

1 Like

GetPlayerFromCharacter is a function of the player service, do game:GetService("Players"):GetPlayerFromCharacter(touching.Parent) instead
EDIT: @lanjtlike just saw your post ;-;

2 Likes

I got an error when I tried that,
Workspace.HO sign.SurfaceGui.Script:6: attempt to index nil with ‘Parent’ - Server - Script:6