Stupid Problem with Events (why!?)

I swear I’ve had this issue before but I forgot why. When I fire some data over the Client/Server boundary, I get this error: “carName is not a valid name of Player “players.Cyricli””

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local PurchaseCar = Events:WaitForChild("PurchaseCar")
local attrName = nil

PurchaseCar.OnServerEvent:Connect(function(carData)
	local carName = carData.carName
	local carPrice = carData.carPrice
	local carPurchaser = carData.carPurchaser
	local cash = carPurchaser:GetAttribute("Cash")
	if carPrice < cash then
		carPurchaser:SetAttribute("Cash", cash - carPrice)
		if carName == "2008 Toyota Camry LE" then
			attrName = "Car2008CamryLE"
		elseif carName == "2018 Toyota Camry XSE" then
			attrName = "Car2018CamryXSE"
		elseif carName == "2023 BMW X6" then
			attrName = "Car2023BMWX6"
		elseif carName == "1998 Toyota Supra" then
			attrName = "Car1998Supra"
		end

		carPurchaser:SetAttribute(attrName, true) -- Assuming you want to set the attribute to true when the car is purchased
	end
end)

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local PurchaseCar = Events:WaitForChild("PurchaseCar")
local Vehicles = ReplicatedStorage:WaitForChild("Cars")
local UIMain = script.Parent
local List = UIMain.List
local ScrollingFrame = List.ScrollingFrame
local selectedCar = nil
local selectedImage = nil
local CarName = List.CarName
local CarTS = List.CarTS
local CarPrice = List.CarPrice
local player = game.Players.LocalPlayer

local function formatCurrency(amount)
	local formattedAmount = string.format("$%s", tostring(amount):reverse():gsub("(%d%d%d)", "%1,"):reverse())
	return formattedAmount
end


local functionToConnect = function(buttonText)
	if selectedImage ~= nil then
		selectedImage.Visible = false
	end
	selectedCar = buttonText
	selectedImage = List.Images:WaitForChild(selectedCar)
	selectedImage.Visible = true
	CarName.Text = selectedCar
	CarTS.Text = "Top Speed: " .. selectedImage.TopSpeed.Value .. "mph"
	CarPrice.Text = "Price: " .. formatCurrency(selectedImage.Price.Value)
end

local function connectButton(button)
	button.MouseButton1Click:Connect(function()
		local buttonText = button.Text
		functionToConnect(buttonText)
	end)
end

for _, button in ipairs(ScrollingFrame:GetChildren()) do
	if button:IsA("TextButton") then
		connectButton(button)
	end
end

List.Interaction.MouseButton1Click:Connect(function()
	local carData = {
		carName = selectedCar,
		carPrice = selectedImage.Price.Value,
		carPurchaser = player
	}

	PurchaseCar:FireServer(carData)
end)

image

2 Likes

First argument that .OnServerEvent provides is player instance that fired it. Add it before the "carData’:
PurchaseCar.OnServerEvent:Connect(function(Player, carData)

3 Likes

Yup… had this realisation about 5 seconds after posting, of course it is. Thank you so much though!

1 Like

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