Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to use LocalPlayer?

Asked by 4 years ago

I have made a script which when you put an "Empty Cup" on a part it turns into "Coke". This is great however I have scripted it so it only works for me and that's bad here is the script:

function tch(h)
if (h.Parent.Name == "Empty Cup") then
h.Parent.Name = "Coke"
script.Parent.Parent.Spill.Transparency = 0.1
wait(0.3)
game.Workspace.dinoroar1.Coke.Handle.Full.Transparency = 0
wait(0.2)
script.Parent.Parent.Spill.Transparency = 1

end
end

script.Parent.Touched:connect(tch)

I really need it to work for anyone who touches it with an Empty Cup someone please help!

0
It should work for everyone??? RealTinCan 217 — 4y
0
Where in the script does it require it to be you? RealTinCan 217 — 4y
0
Line 6, I put "game.Workspace.dinoroar1.Coke.Handle.Full" dinoroar1 is me User#29585 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Assuming this is a localscript, put in this code:

local character = game:GetService("Players").LocalPlayer.Character -- i used local player for identifying the player

function tch(h)
if (h.Parent.Name == "Empty Cup") then
h.Parent.Name = "Coke"
script.Parent.Parent.Spill.Transparency = 0.1
wait(0.3)
character.Coke.Handle.Full.Transparency = 0
wait(0.2)
script.Parent.Parent.Spill.Transparency = 1

end
end

script.Parent.Touched:connect(tch)

if it is a serverscript, check out devrbxpage/GetPlayerFromCharacter for details!

0
But this might not work everytime. The script starts running when a player joins a server and that can be before the character has loaded. So the variable Character can return nil sometimes. noammao 294 — 4y
0
That can be solved by using the line: Player.Character or Player.CharacterAdded:Wait() Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by
noammao 294 Moderation Voter
4 years ago
Edited 4 years ago

LocalPlayer is used in codes. when used it refers to you as a player. It's the equivalent of game.Players.Your_name but it can only be accessed through local scripts. This is useful when you want to create individual changes without creating a system that takes the players username into consideration.

On line 6 you use your own character name but what you could use to make it available to everyone is to type game.Players.LocalPlayer.Character.Coke.Handle.Full.Transparency = 0.

Hopefully, this should work if you put it in a local script.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

function tch(h)
if (h.Parent.Name == "Empty Cup") then
h.Parent.Name = "Coke"
script.Parent.Parent.Spill.Transparency = 0.1
wait(0.3)
Character.Coke.Handle.Full.Transparency = 0
wait(0.2)
script.Parent.Parent.Spill.Transparency = 1

end
end

script.Parent.Touched:connect(tch)
0
It doesn't work in a local script it says "Touched is not a valid member of Model" User#29585 0 — 4y
1
That is because you can't use .Touched on a model.. duh greatneil80 2647 — 4y

Answer this question