Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fix this function script with finding a players folder?/?

function Hit()
	local p = game.Players:GetPlayerFromCharacter(Hit.Parent) .Folder.rank == "HighBorn"
	if p == "HighBorn"
		then game.Workspace.Union.CanCollide = false
		else print("U aint old enough")
	end
end

script.Parent.ClickDetector.MouseClick:connect(Hit)

Workspace.Part.Script:2: attempt to index global 'Hit' (a function value)

I've tried fixing myself but no progress.

2 answers

✓ Accepted answer

First off you had a space where you wouldn't want one

Old

local p = game.Players:GetPlayerFromCharacter(Hit.Parent) .Folder.rank == "HighBorn"

New

local p = game.Players:GetPlayerFromCharacter(Hit.Parent).Folder.rank == "HighBorn"

Also, hit is just a function, so we cant call it when using getplayerfromcharacter because hit isnt the character. You are thinking of ontouched and hit would be inside the () like function touched(HIT) but in this clickdetector script player is inside the () like function clicked(player)

next one line 2 if highborn isn't there it'll result in an error.

So we will check if its there BEFORE we set it.

if player:FindFirstChild("Folder")~=nil and player.Folder:FindFirstChild("Rank")~=nil then

and im assuming that rank is a stringvalue so we cant just do if rank=="highborn" we need to do

if rank.Value=="HighBorn"

so if we fix all of these problems this is the script we will come up with.

function clicked(player)
if player:FindFirstChild("Folder")~=nil and player.Folder:FindFirstChild("Rank")~=nil then
if player.Folder.Rank.Value=="HighBorn" then
workspace.Union.CanCollide=false
else
print("U aint oldenough")
end
end
end

script.Parent.ClickDetector.MouseClick:connect(clicked)

Hoped this helped :)

The MouseClick event of ClickDetectors already returns the player who clicked. You can reference it easily by setting up a parameter.

But, why you're getting the error is because you're trying to get the parent of 'Hit' when Hit isn't an object! It's a function. There is no parent.

I'm not enitrely sure what you were trying to do with line 2 but just check it with an if statement, like normal.

function Hit(plr)
	local p = plr:FindFirstChild("Folder").rank 
	if p.Value == "HighBorn" then 
		workspace.Union.CanCollide = false
	else
		 print("U aint old enough")
	end
end

script.Parent.ClickDetector.MouseClick:connect(Hit)

This discussion is preserved as it was. New answers, comments, and votes are closed.