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

How to use LocalPlayer?

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!

3 comments

0

It should work for everyone???

0

Where in the script does it require it to be you?

0

Line 6, I put "game.Workspace.dinoroar1.Coke.Handle.Full" dinoroar1 is me

2 answers

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!

2 comments

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.

0

That can be solved by using the line: Player.Character or Player.CharacterAdded:Wait()

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)

2 comments

0

It doesn't work in a local script it says "Touched is not a valid member of Model"

1

That is because you can't use .Touched on a model.. duh

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