How to make a localscript run when a player joins

here is my script:

local doorsfolders = game.Workspace.Map.Doors
local attempts = 0
local alreadytookprice = false
local uis = game:GetService("UserInputService")

for i, v in pairs(doorsfolders:GetChildren()) do
	attempts = 1
	if (v.EButtonPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude <= 30 then
		script.Parent.Parent.Parent:FindFirstChild(v.Name.."Gui").Enabled = true
		script.Parent.Parent.Parent:FindFirstChild(v.Name.."Gui").E.MouseButton1Click:Connect(function()
			script.Parent.Visible = true
			script.Parent.Title.Value = v.Name
			script.Parent.Price.Value = v.Needed.Value
			script.Parent.Yes.MouseButton1Click:Connect(function()
				if game.Players.LocalPlayer.leaderstats.Orbs.Value >= script.Parent.Price.Value then
					if alreadytookprice == false then
						alreadytookprice = true
						game.Players.LocalPlayer.leaderstats.Orbs.Value -= script.Parent.Price.Value
						print("already took price")
						wait(1)
						alreadytookprice = false
					end
					v:Destroy()
					script.Parent.Visible = false
					if attempts == 1 then
						local result = game.ReplicatedStorage.EggHatchingRemotes.Door:InvokeServer(v.Name)
						attempts = 0
					end
				end
			end)
			script.Parent.No.MouseButton1Click:Connect(function()
				script.Parent.Visible = false
			end)
		end)
		uis.InputBegan:Connect(function(key)
			if key == Enum.KeyCode.E then
				script.Parent.Visible = true
				script.Parent.Title.Value = v.Name
				script.Parent.Price.Value = v.Needed.Value
				script.Parent.Yes.MouseButton1Click:Connect(function()
					if game.Players.LocalPlayer.leaderstats.Orbs.Value >= script.Parent.Price.Value then
						if alreadytookprice == false then
							alreadytookprice = true
							game.Players.LocalPlayer.leaderstats.Orbs.Value -= script.Parent.Price.Value
							wait(1)
							alreadytookprice = false
						end
						v:Destroy()
						script.Parent.Visible = false
						if attempts == 1 then
							local result = game.ReplicatedStorage.EggHatchingRemotes.Door:InvokeServer(v.Name)
							attempts = 0
						end
					end
				end)
				script.Parent.No.MouseButton1Click:Connect(function()
					script.Parent.Visible = false
				end)

			end
		end)
	else
		script.Parent.Parent.Parent.CityGui.Enabled = false
	end
end

Im trying to figure out how I could make this script only run when the player join so its not running before because on the line

	if (v.EButtonPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude <= 30 then

It says HRP is not a valid member of character

1 Like

I think you could just make it

if (v.EButtonPart.Position - game.Players.LocalPlayer:WaitForChild("Character").HumanoidRootPart.Position).Magnitude <= 30 then

Infinite yield possible on ‘Players.dxvblake:WaitForChild(“Character”)’ … I get an error

what the hell is all of that???

just do this:

local players = game.Players


players.PlayerAdded:Connect(function()
	
	--insert code here
	
end)

It’s not an error message, it’s a warning, it will continue the script after the Character is loaded.

I think what he’s looking for is for the CharacterAdded event tbh

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
	end)
end)
1 Like

its a localscript though they run automatically

1 Like

its a localscript they run auto

1 Like

Ok, try this:

local doorsfolders = game.Workspace.Map.Doors
local attempts = 0
local alreadytookprice = false
local uis = game:GetService("UserInputService")
local localplr = game.Players.LocalPlayer

for i, v in pairs(doorsfolders:GetChildren()) do
	attempts = 1
	if (v.EButtonPart.Position - localplr:WaitForChild("Character"):WaitForChild("HumanoidRootPart").Position).Magnitude <= 30 then
		script.Parent.Parent.Parent:FindFirstChild(v.Name.."Gui").Enabled = true
		script.Parent.Parent.Parent:FindFirstChild(v.Name.."Gui").E.MouseButton1Click:Connect(function()
			script.Parent.Visible = true
			script.Parent.Title.Value = v.Name
			script.Parent.Price.Value = v.Needed.Value
			script.Parent.Yes.MouseButton1Click:Connect(function()
				if localplr.leaderstats.Orbs.Value >= script.Parent.Price.Value then
					if alreadytookprice == false then
						alreadytookprice = true
						localplr.leaderstats.Orbs.Value -= script.Parent.Price.Value
						print("already took price")
						wait(1)
						alreadytookprice = false
					end
					v:Destroy()
					script.Parent.Visible = false
					if attempts == 1 then
						local result = game.ReplicatedStorage.EggHatchingRemotes.Door:InvokeServer(v.Name)
						attempts = 0
					end
				end
			end)
			script.Parent.No.MouseButton1Click:Connect(function()
				script.Parent.Visible = false
			end)
		end)
		uis.InputBegan:Connect(function(key)
			if key == Enum.KeyCode.E then
				script.Parent.Visible = true
				script.Parent.Title.Value = v.Name
				script.Parent.Price.Value = v.Needed.Value
				script.Parent.Yes.MouseButton1Click:Connect(function()
					if localplr.leaderstats.Orbs.Value >= script.Parent.Price.Value then
						if alreadytookprice == false then
							alreadytookprice = true
							localplr.leaderstats.Orbs.Value -= script.Parent.Price.Value
							wait(1)
							alreadytookprice = false
						end
						v:Destroy()
						script.Parent.Visible = false
						if attempts == 1 then
							local result = game.ReplicatedStorage.EggHatchingRemotes.Door:InvokeServer(v.Name)
							attempts = 0
						end
					end
				end)
				script.Parent.No.MouseButton1Click:Connect(function()
					script.Parent.Visible = false
				end)

			end
		end)
	else
		script.Parent.Parent.Parent.CityGui.Enabled = false
	end
end

Nope, still doesnt work. I get the warning

Ignore the damn warning, it just says it could stay in a loop if the Character/HRP never loads.
The script should work as intended.

1 Like

It doesn’t work when I do that

This wont work as PlayerAdded in a localscript doesnt work for the first player to join the server

I found out what was wrong there

Would this work?

if game.Players:FindFirstChild(game.Players.LocalPlayer.Name) then
    
end
1 Like

For future reference; are you able to add what was wrong with your code? So if another user has a similar problem they can refer to this post.

3 Likes