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

How do I make it so players can click on other players?

I am trying to make police system where a police can click on a criminal's body and a gui appears on both the cop and the criminals screen (2 separate guis already made). I made the click detector (not using an actual click detector) and the 2 print statements in my code work, but I keep getting the error message "expected string, got instance" so Im wondering what I did wrong in my code. Thanks!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local gui = game.StarterGui.PoliceGUI.search --search is the frame I want to show for cop

local target -- just creating a variable

mouse.Button1Down:Connect(function()
	target = mouse.Target.Parent
	print(target) -- this prints anything the player clicks on (but the parent)
	
	local humanTarget = target:FindFirstChild("Humanoid") -- checks if target has humanoid
	
	if humanTarget ~= nil then
		print("humanoid found") -- this works as well
		
		local playergui = game.Players[target]:FindFirstChild("PlayerGui")
		playergui.PoliceGUI.searchPrompt.Visible = true -- this frame shows for criminal
		gui.Visible = true -- sets cop frame to visible
	end
	
end)

2 answers

on game.Players[target]... do game.Players[target.Name] . Doing instance[variableObjectName] searches for the name of the object.

local playergui = game.Players[target.Name]:FindFirstChild("PlayerGui")

6 comments

0

thanks im going to try it out right now

0

Players.Player2.Backpack.LocalScript:18: attempt to index nil with 'PoliceGUI'

0

the playergui variable is appearing as nil which means that line isn't working. Idk why tho

0

Are you sure 'PoliceGUI' is where you are referencing it? Does it actually exist where you say it does in the script?

0

Attempted to index nil with... means it doesn't exist.

0

btw when I do PlayerGui.PoliceGUI it says attempted to associate a nil value WITH PoliceGUI which means the PlayerGui folder inside the player is nil (nothing)

You are attempting to make the player's mouse target something that has been restricted from being targeted using target filter. First you need to change the mouse's TargetFilter to nil so that it can target everything and then you can check for a humanoid and use the function GetPlayerFromCharacter to get the player and change their gui. Also you tried to change your own gui by selecting it in starter gui which doesnt exist once the game starts.

Here is how you would go by doing this:

Put this script into a localScript in the StarterPlayerScripts section.

-- Services --
local Players = game:GetService('Players')
local SG = game:GetService('StarterGui')

local player = Players.LocalPlayer
local mouse = player:GetMouse()

-- Detects if mouse button is pressed --
mouse.Button1Down:connect(function()
	
	mouse.TargetFilter = nil -- the default targetfilter is set to player which means mouse.Target will never encounter a player
	
	if mouse.Target then -- checks if mouse.Target exists
		print(mouse.Target)
		
		if mouse.Target.Parent:FindFirstChild('Humanoid') then -- checks for humanoid in order to determine player
			print('Player Found.')
			
			local targetPlayer = Players:GetPlayerFromCharacter(mouse.Target.Parent)
			-- sets clicked player
			
			targetPlayer.PlayerGui.PoliceGUI.search.Visible = true
			-- sets target player's gui visible
			
			player.PlayerGui.PoliceGUI.search.Visible = true
			-- sets own gui visible	
		end
	end
end)

Hope this helped, try this out and let me know.

2 comments

0

Thanks so much im trying it out now Il let you know if it worked. My old script was also in starterpack so I dont know if that had something to do with it

0

It didnt work it said it couldnt find playerGui inside of the player

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