Help with using functions

So i am fairly new to using lua, and in my game i am making, i need to use remote events to trigger a function. However the point of using that remote event is to store the selected part in a variable on a local script

So far it looks like this and doesn’t seem to be working

currentx = nil
currenty = nil
currentnum = nil

replistore.tileselect.OnClientEvent:Connect(function(v)
	print('number is:',v:GetAttribute('number'))
	currentx = v:GetAttribute('xcoord')
	currenty = v:GetAttribute('ycoord')
	currentnum = v:GetAttribute('number')
end)

Can anyone help me with what im doing wrong? The variables don’t appear to update when the function runs

could you send the server part of the code?
What exactly is v?
You should name it something that describes what it is.

Try this code out and let me know how it works out for you

Remote event:

-- ServerScriptService: Listen for part selection
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local partSelectedEvent = ReplicatedStorage:WaitForChild("PartSelectedEvent")

-- Function to handle part selection
partSelectedEvent.OnServerEvent:Connect(function(player, selectedPart)
    print(player.Name .. " selected the part: " .. selectedPart.Name)
    -- You can perform additional logic here if needed
end)

Coding for the remote event:

-- Local Script: Detecting part selection and sending it to the server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local partSelectedEvent = ReplicatedStorage:WaitForChild("PartSelectedEvent")

local selectedPart -- Variable to store the selected part

-- Example: Detecting part click
local function onPartClicked(part)
    selectedPart = part -- Store the part in the variable
    print("Part selected: " .. part.Name)

    -- Send the selected part to the server via RemoteEvent
    partSelectedEvent:FireServer(part)
end

-- Example: Bind part selection (for demonstration)
for _, part in pairs(workspace:GetDescendants()) do
    if part:IsA("BasePart") then
        part.Touched:Connect(function()
            onPartClicked(part) -- Trigger the selection when part is touched
        end)
    end
end

Oh sorry, v is just a part, and it has attributes which are the number which it has on it, and co-ordinates. I have checked and this is passing through into the client script just fine

This is the current server script code and it does work

for _,v in ipairs(puzgrid:GetChildren()) do
	if v:IsA("Part") then
		v.SurfaceGui.TextLabel.Text = numtable[count]
		v:SetAttribute('number',numtable[count])
		v.ClickDetector.MouseClick:Connect(function()
			replistore.tileselect:FireAllClients(v)
			local highlight = Instance.new('Highlight')
			highlight.Parent = v
			highlight.Adornee = v
			highlight.FillColor = Color3.new(0.952941, 0.996078, 1)
			print('Clicked')
			if prevv ~= nil then
				prevv.Highlight:Destroy()
			end
			prevv = v
		end)
		count += 1
	end
end

To clarify, i want to get the instance and pass it from server script to local script, and then store that in a variable

I figured out the problem. I just embedded the other part of my script inside the remote event function and that worked

2 Likes

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