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

How do I make a script that uses Players.LocalPlayers work in-game instead of Studio?

I'm trying to make a trash can script, and to not accidentally delete your items, the trash can will ask for confirmation via GUI. The GUI is placed in the StarterGUI. It works perfectly in Studio, but not Online. Can someone tell me why or try to fix the current script?

I've tried Scripts and LocalScripts! Nothing is working.

function onTouch()
	game.Players.LocalPlayer.PlayerGui.DeleteInv.Frame.Visible = true
end
script.Parent.Touched:connect(onTouch)

1 comment

0

You have a beautiful answer below

1 answer

LocalScripts don't run in workspace


You can't use LocalPlayer in a regular script in-game, so you'll need a local script. Simply move the LocalScript into the GUI in StarterGui. All you'll need to do then is redefine the part in workspace.

local plr = game.Players.LocalPlayer
local frame = script.Parent -- Assuming you put the local script inside the frame

local function onTouch(hit)
	if(game.Players:GetPlayerFromCharacter(hit.Parent)==plr)then -- is the local player the one that touched the part?
		frame.Visible = true
	end
end

workspace:WaitForChild("Part").Touched:connect(onTouch)

2 comments

0

Nice. :) Although, why not define line 10 before connecting the event? lol

0

Because it literally doesn't matter.

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