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

I need to learn about OnTouched Functions and variaibles?

I need help

local player = game.Players.LocalPlayer
local text = player.PlayerGui.PlaceGui.MainFrame:WaitForChild("TextShower")
local part = script.Parent

part.Touched:connect(function(hit)
	text.Text = 'Testing'
end)

this is a script inserted into a part the error is on line 2 Workspace.Test.Script:2: attempt to index local 'player' (a nil value) I tried fixing it but I don't know how. Help

5 comments

0

game.Players.LocalPlayer can only be accessed from a LocalScript. It's nil inside of a Script.

0

"Touched" is an event not a function.

0

^

0

Is this a local script

0

Next time use the code block feature. It's the Lua Button

1 answer

✓ Accepted answer

##Problem

Seeing as your code is located in Workspace, I am assuming this is a server script. In which case, ServerScripts can not access LocalPlayer. This is one of the more common mistakes I have cited in my blog article.


##Solution

There are two ways of going about this.

  1. Using a LocalScript
  • You can rename the part in Workspace to something unique. Whenever the character hits the part then the GUI elements can change. This is actually recommended as you're dealing with GUI elements which should be handled by the clients. This method is also compatible with FilteringEnabled environments.
  1. Using a ServerScript
  • You can keep the script in the normal location and gather the player from the GetPlayerFromCharacter method. Once gathering the player you can go into the PlayerGui and modify the text.

##Final Script

####Solution 1 (LocalScript Method):

LocalScripts will only work in PlayerGui, a Player's Backpack, PlayerScripts, or the Player's Character.

game.Workspace.PartNameHere.Touched:Connect(function(partThatTouched) --Fire a function every time the Touched event fires. partThatTouched is the part that touched the brick.
    if partThatTouched.Parent == game.Players.LocalPlayer.Character then --Make sure it is the character hitting the brick and not some other brick.
        game.Players.LocalPlayer.PlayerGui.PlaceGui.MainFrame.TextShower.Text = "Testing" --Change the text. You can decide how the vairables are set.
    end
end)

####Solution 2 (ServerScript Method):

script.Parent.Touched:Connect(function(partThatHit)
    local player = game.Players:GetPlayerFromCharacter(partThatHit.Parent) --Function will return the player object. This is gathered from the character from the parent of the partThatHit.
    if player then --Check to see if the player exists or if some random brick hit the part.
        player.PlayerGui.PlaceGui.MainFrame.TextShower.Text = "Testing" --Change the text.
    end
end)

######Hopefully this answered your question. If it did, hit the accept answer button. If you have any further questions, post them in the comments below and I will get back to you.

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