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

How to make onTouch check for something in the localplayer ?

How you do it ? I get error Workspace.Part.Script:5: attempt to index upvalue 'player' (a nil value)

local player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if player.PlayerGui:FindFirstChild("Test") then
			hit.Parent.Humanoid.Health = 0
		else
			print("Thing not found, yay")
		end
	end
end)

1 comment

0

The error does not match this code? Where is 'upvalue'

1 answer

You can always put a Script inside of the part and do like this

script.Parent.Touched:connect(function(hit)
	local player = game.Players:FindFirstChild(hit.Parent)

	if player ~= nil then
		if player.PlayerGui:FindFirstChild("Test") then
			hit.Parent.Humanoid.Health = 0
		else
			print("Thing not found, yay")
		end
	end
end)

A LocalScript will only run Lua code if it is a descendant of one of the following objects:
A Player's Backpack, such as a child of a Tool
A Player's Character model
A Player's PlayerGui
A Player's PlayerScripts
The ReplicatedFirst service
Note: the parent of the LocalScript will determine which client the Lua code will be executed on.

Also, LocalPlayer can only be referenced from the client, via LocalScript. Thus trying to call LocalPlayer anywhere on the server will not work.

More info: http://wiki.roblox.com/index.php?title=API:Class/LocalScript

3 comments

0

Doesn't work.

0

Is it a SCRIPT, not LocalScript inside of the part?

0

I promise you that what I have above works. 100% just tested it. A script inside a part in workspace, with any item named 'Test' inside of PlayerGui.

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