Why is the value set as nil?

On the event, this would change the text of a text label to a set of values(a string and an integer) both set in another part. value1 is the integer and value2 is the string. When I run it, it gives the correct value for value1 but it says that value2 is nil. I tried taking value2 out of tosring since it is a string already but it gave me an error that said, “attempt to concatenate nil with string”.

local event = game:GetService("ReplicatedStorage"):WaitForChild("??")
local label = script.Parent.Frame.TextLabel

event.OnClientEvent:Connect(function(player, value1, value2)
    label.Text = "Do you want to buy "..tostring(value2).." for "..tostring(value1).." coins?"
	script.Parent.Visible = true
end)

We couldn’t tell you why it’s nil from this code alone. We need to see where the event is fired.

The event is fired from a part in the workspace. It fires the event once someone touches it. This part is also where the values are stored. Here’s the script for it:

local function onTouch(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum ~= nil then
		if not debounce then
			debounce = true
			local value1 = script.Parent.Strng.Value
			local value2 = script.Parent.Int.Value
			local player  = game.Players:GetPlayerFromCharacter(part.Parent)
			event:FireClient(player, value1, value2)
			wait(1)
			debounce = false
		end
		end
end

script.Parent.Touched:Connect(onTouch)
1 Like

.OnClientEvent() doesn’t have the player who received the signal as the first argument. This is only the case for .OnServerEvent() connections. So, only value1 and value2 are the arguments. In fact, if you print player you may find it has the value of value1.

Having only the following should solve the issue:

event.OnClientEvent:Connect(function(value1, value2)
2 Likes

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