Attempt to concatenate nil with string

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Whenever a play collects an egg, an event fires. It sends the play name and the egg name to the client. The client writes a congratulations message.

  1. What is the issue? Include screenshots / videos if possible!
Players.fest1vities.PlayerGui.ScreenGui.LocalScript:25: attempt to concatenate nil with string

The script keeps erroring insisting that the values for the name or eggname is nil. Even though in the output, prints just fine.

CLIENT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GiveEggEvent = ReplicatedStorage:WaitForChild("giveegg")

local function createEggFoundFrame(player, eggName)
	

	-- create the GUI frame
	local frame = Instance.new("Frame")
	frame.Size = UDim2.new(0, 300, 0, 150)
	frame.Position = UDim2.new(0.5, -150, 0.5, -75)
	frame.BackgroundColor3 = Color3.new(1, 1, 1)
	frame.BackgroundTransparency = 0.5
	frame.BorderSizePixel = 0
	frame.Parent = game:GetService("Players").LocalPlayer.PlayerGui.ScreenGui

	-- create the "Congratulations" label
	local congratsLabel = Instance.new("TextLabel")
	congratsLabel.Size = UDim2.new(1, 0, 0, 50)
	congratsLabel.Position = UDim2.new(0, 0, 0, 0)
	congratsLabel.Font = Enum.Font.SourceSansBold
	congratsLabel.TextColor3 = Color3.new(0, 0, 0)
	congratsLabel.TextSize = 20
	congratsLabel.BackgroundTransparency = 1
	congratsLabel.Parent = frame
	congratsLabel.Text = "Congratulations " .. player.Name .. "! You found the " .. eggName  .. "!"

	-- tween the frame to scale up and fade in
	frame:TweenSizeAndPosition(UDim2.new(0, 600, 0, 300), UDim2.new(0.5, -300, 0.5, -150), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 1, true)
	frame.BackgroundTransparency = 0
end

GiveEggEvent.OnClientEvent:Connect(function(player, eggName)
	createEggFoundFrame(player, eggName)
	
end)

SERVER

local part = script.Parent
local clickDetector = part.ClickDetector

clickDetector.MouseClick:Connect(function(player)
	-- disable the click detector to prevent the part from being clicked again
	clickDetector:Destroy()
	part.Anchored = false

	-- create a BodyVelocity object to make the part fly up into the sky
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.Velocity = Vector3.new(0, 100, 0)
	bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVelocity.P = 5000
	bodyVelocity.Parent = part

	-- wait for 1 second before exploding the part
	wait(1)

	-- create an explosion at the part's position
	local explosion = Instance.new("Explosion")
	explosion.BlastRadius = 0
	explosion.Position = part.Position
	explosion.Parent = part.Parent

	-- fire the "giveegg" event in ReplicatedStorage with the player and egg name as arguments
	local eggName = "SILLY_EGG" -- replace this with the name of your egg
	local replicatedStorage = game:GetService("ReplicatedStorage")
	local giveEggEvent = replicatedStorage:FindFirstChild("giveegg")
	if giveEggEvent then
		print("Firing giveegg event for player " .. player.Name .. " and egg " .. eggName)
		giveEggEvent:FireClient(player,eggName)


	end

	-- destroy the part and the body velocity object
	part:Destroy()
	bodyVelocity:Destroy()
end)

Again, on the server when it prints into the output, it correctly prints the eggs name and the players name.
image

Any help would be greatly appreciated!

1 Like

when the client listens to remote events, there is no player parameter since it is local anyways. so you only have 1 parameter and that is eggname (in your current code the 2nd parameter becomes nil). you have to change the line to:
GiveEggEvent.OnClientEvent:Connect(function(eggName)
createEggFoundFrame(eggName)

end)

3 Likes

Words cannot describe how greatful I am rn, I spent a solid hour and a half on this. Thank you so much lol.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.