Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you use remote events?

To be more specific, i'm trying to make a game (again XD). I was trying to send multiple augments from a server script to a local script. I got this error from the server script : Unable to cast value to Object And this error from the local script : attempt to call field 'OnClientEvent' (a userdata value)

Code in server script :

	for i,v in pairs(game.ReplicatedStorage.Maps:GetChildren()) do --This part chooses the map, mybe a gamemode aswell, someday
		if not ChoseMapBolean then
			local RandomMapValue = math.random(1,2)
			if RandomMapValue == 2 then
				print(v)
				ChosenMap = v
				ChoseMapBolean = false
				script.RemoteEvent:FireClient({v , "ForMap"}) -- The first part of are table is the value, the second is what its for and before you say "OMG lamo wat if al da math.randoms are 1" I have a fix for that
			end
		end
	end

Code in local script :

game.ServerScriptService.GameScript.RemoteEvent.OnClientEvent(function(Aguments)
	if Aguments[2] == "ForMap" then 
		SG.VoteForMap.JoinButton.Text = "Classic | " .. Aguments[1] .. " | PLayers : idk"
	end
end)

1 comment

1

I made a video specifically for explaining this that should answer your question: https://www.youtube.com/watch?v=4Dc_bri9mjs

1 answer

✓ Accepted answer
  • OnClientEvent is an Event object
    You'll need to treat it just like any other event and use the connect method on it in order to connect a function to it as a listener. At the moment you are simply calling it, which is causing the second error you are getting.

  • You never said which client
    When you use FireClient you need to supply the Player to send the event to as the first argument. If you want to fire the event for all players, you can simply use FireAllClients instead which will work how you are expecting and does not require a player.

This discussion is preserved as it was. New answers, comments, and votes are closed.