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

Issue with click detector?

script.Parent.ClickDetector.MouseClick:connect(function(p)
	local player = p
	local gui = game.ReplicatedStorage.ATMGUI:Clone()
	local pplayer = game.Players:GetPlayerFromCharacter(p)
	gui.Parent = game.Players.LocalPlayer.PlayerGui
	gui.Frame.Visible = true
	gui.Frame:TweenPosition(UDim2.new(0.5, -150,.5, -75), "Out", "Quad", .5)

end)

Can someone help me figure out why this isn't working? Thanks.

2 answers

Put the following code in a local script. You can place the local script in StarterPlayerScripts. Edit/modify to your needs.

local plr = game.Players.LocalPlayer
local pg = plr:WaitForChild("PlayerGui")
local gui = game.ReplicatedStorage:WaitForChild("ATMGUI")
local somePart = workspace:WaitForChild("TheNameOfYourPart")
local clickDetector = somePart:WaitForChild("ClickDetector")

clickDetector.MouseClick:connect(function()
	local char = plr.Character
	if char then
		local hum = char:FindFirstChild("Humanoid")
		if hum and hum.Health > 0 then
			local clone = gui:Clone()
			clone.Parent = pg
			local frame = clone:WaitForChild("Frame")
			frame.Visible = true
			frame:TweenPosition(UDim2.new(0.5, -150, .5, -75), "Out", "Quad", .5)
		end
	end
end)

Hopefully that works!

Not to bash on Instawin, but he isn't really answering what is wrong with your code and is giving you an entirely different solution to the problem.

So let's break this down line by line:

script.Parent.ClickDetector.MouseClick:connect(function(p)

This connects the MouseClick event to a un-named function with the parameter p The parameter p "holds" the player that clicked it, not the character, but their player.

local pplayer = game.Players:GetPlayerFromCharacter(p)

This variable you have defined is attempting to the the Player from a character object. The passed parameter to the GetPlayerFromCharacter() function requires a character object, but as we have looked at above, p is actually a player object. Thus, this line is not needed.

gui.Parent = game.Players.LocalPlayer.PlayerGui

LocalPlayer is a reserved variable that points to the Local Player. What that means is: LocalPlayer points to the player that is running the script on their client. However, your script is a child (found in) of the Workspace, meaning it is a server-side script, not a client-side script. Client-side scripts run within a Player object itself (like its PlayerGui). Client-side scripts are also LocalScript objects, not regular scripts. In conclusion, the error here is that you are trying to access a client-side variable from a server-side script.

So, to fix up the script you gave:

script.Parent.ClickDetector.MouseClick:connect(function(p)
    local gui = game.ReplicatedStorage.ATMGUI:Clone()

    gui.Parent = p.PlayerGui -- Using what I said above, this is the player's PlayerGui

    gui.Frame.Visible = true
    gui.Frame:TweenPosition(UDim2.new(0.5, -150,.5, -75), "Out", "Quad", .5)
end)

Further resources you can look at:

ClickDetector.MouseClick

getPlayerFromCharacter

LocalPlayer

LocalScript

Client vs Server

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