Need help with my mailbox script

Hi guys,

It’s me again :slight_smile:
I’m in the process of scripting a postman job.
It works in such a way that a value in the mailbox is gradually increased up to 15. If you then click on this, the value is set in the value by the player. Then this is set to 0. Everything worked so far.

Now, however, I still wanted feedback if the mailbox is not full enough or if letters have been removed. As soon as I have inserted the code for it, nothing works anymore

My Code:

local BVal = script.Parent.Parent.BVal
local plr = game.Players.LocalPlayer
local label = game.Players.LocalPlayer.PlayerGui.Info.Textlabel

script.Parent.MouseClick:Connect(function(plr)
	Take()
end)

function Take()
	if BVal.Value <= 5 then
		TypeWrite(label, "The mailbox is empty")
	elseif BVal.Value >= 5 and plr.Job.Briefe.Value <= 29 then
		plr.Job.Briefe.Value = plr.Job.Briefe.Value + BVal.Value
		TypeWrite(label, "You took "..BVal.Value.." letters")
		BVal.Value = 0
	end
end

function TypeWrite(object, text)
	for i = 1, #text, 1 do
		object.Text = string.sub(text, 1, i)
		wait(0.025)
	end
end

while true do
	for i = 1, 5 do
		wait(1)
		if BVal.Value < 15 then
			BVal.Value = BVal.Value + 1
		end
	end
end

I’ve already tried a few things to fix it, unfortunately nothing has helped. Now I hope again for your help :slight_smile:

Thank You
Pino

Do you get any errors in the output?

Right, I completely forgot. I have an error in the output:

  12:21:21.344  Workspace.Post-Briefkasten.ClickDetector.Script:3: attempt to index nil with 'PlayerGui'  -  Server - Script:3
  12:21:21.344  Stack Begin  -  Studio
  12:21:21.344  Script 'Workspace.Post-Briefkasten.ClickDetector.Script', Line 3  -  Studio - Script:3
  12:21:21.344  Stack End  -  Studio

PlayerGui might take a while to replicate on the client. Try adding a :WaitForChild:

local label = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("Info"):WaitForChild("TextLabel")

Also in your script, you are calling functions that haven’t been defined yet. Perhaps that’s an issue too?

1 Like

Now the error is as follows:

  12:27:24.910  Workspace.Post-Briefkasten.ClickDetector.Script:3: attempt to index nil with 'WaitForChild'  -  Server - Script:3
  12:27:24.911  Stack Begin  -  Studio
  12:27:24.911  Script 'Workspace.Post-Briefkasten.ClickDetector.Script', Line 3  -  Studio - Script:3
  12:27:24.911  Stack End  -  Studio

You cannot utilize LocalPlayer in a server script.

3 Likes

I think I’ve found the issue. If I am correct, you are calling LocalPlayer in a server script, that returns nil. Use the Player service instead.

I found the solution. My script now looks like this:

local BVal = script.Parent.Parent.BVal


script.Parent.MouseClick:Connect(function(plr)
	local label = plr.PlayerGui.Info.TextLabel
	if BVal.Value <= 5 then
		TypeWrite(label, "The mailbox is empty")
	elseif BVal.Value >= 5 and plr.Job.Briefe.Value <= 29 then
		plr.Job.Briefe.Value = plr.Job.Briefe.Value + BVal.Value
		TypeWrite(label, "You took "..BVal.Value.." letters")
		BVal.Value = 0
	end
end)


function TypeWrite(object, text)
	for i = 1, #text, 1 do
		object.Text = string.sub(text, 1, i)
		wait(0.025)
	end
end

while true do
	for i = 1, 5 do
		wait(1)
		if BVal.Value < 15 then
			BVal.Value = BVal.Value + 1
		end
	end
end
1 Like