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

How To Get The Player In A Normal Script?

Heres My Attempt

--Control Script
local player = script.Parent.Parent
script.Parent.Touched:connect(function(hit)
local tool = game.Lighting.Slide:Clone()
tool.Name = "Control"..hit.Parent.Name
tool.Parent = player.Name
end)

4 comments

0

Un, what's a normal script? Also, you shouldn't use Lighting as storage.

0

can you explain what is going on a little better

0

I edited my answer for you @basstracker1970

0

Please properly indent your code as it makes it easy to read and debug.

4 answers

From what I am reading I think you are using a LocalScript. If so all you need to do is get the local player like so:

local players = game:GetService("Players")
player = players.LocalPlayer

Hope this helps and have a great day scripting!

Edit 1: If the script is a descendant of a player then use a LocalScript. Otherwise you will have to get the player either by looping through the players and applying it to each one, or getting the player via an event such as the PlayerAdded or Touched events. Also as the other answers point out, you should us something other than Lighting for storage and :Connect instead of :connect.

6 comments

0

no u

0

Yes, me

0

im useing a normal script a non local script

0

Also as the other answers point out, you should us something other than lighting for storage and :Connect instead of :connect.

0

@incapaz what do you mean?

0

In one of my recent projects I got the player using a remote event, if that helps.

game.players.playeradded:connect(Function (plr)

end)

plr is now your player.

5 comments

0

Fail.

0

Yea, a total fail, it should work, if it doesn't then someone can correct me...

0

I'll tell you the errors. 1. game.players errors because an object named players isn't inside game. 2. playeradded is not a valid member of Players, 3. connect should be Connect 4. Function should be function 5. there should be no space between function and (plr)

0

Lel, 5 errors in only 2 lines...

0

Wow

Script.Parent.Parent can only be used when you insert the script inside a tool in the StarterPack. Since you're using a Touched event, what you can do is

script.Parent.Touched:connect(function(hit)

if hit.Parent then
 local player = game.Players:GetPlayerFromCharacter(hit.Parent) ---now player is defined
 if player then 
 local tool = game.Lighting.Slide:Clone()
 tool.Name = "Control"..player.Name
 tool.Parent = player.Name
		end
	end
end)

also don't put your script in lighting, i'm just showing you what the script should be like.

Using a thousands of Parent's is bad practicing! You should be using:

script.Parent.Touched:Connect(function(char)
local plr = game:GetService("Players"):GetPlayerFromCharacter(char.Parent)
end)

Also you're using :connect() which is deprecated, you should be using :Connect(). Using a Lighting as your storage is bad, you should be using ReplicatedStorage! Here,

Script:

-- Control Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Put your tool inside a ReplicatedStorage!

script.Parent.Touched:Connect(function(hit)

local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

local tool = ReplicatedStorage:WaitForChild("Slide"):Clone()

tool.Name = "Control"..hit.Parent.Name
tool.Parent = plr.Name
-- What are those two lines up here?^

end)

Hope that I helped you!

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