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

How to call a RemoteEvent through a Touched event script in a Part?

I'm trying to call a RemoteEvent to a player that touched a part but i can't seem to figure out how to get it to call the Event.

Here is the script: local Replicated = game:GetService("ReplicatedStorage") local Radio = Replicated:WaitForChild("Sounds"):FindFirstChild("Radio")

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		Radio:FireClient(hit.Parent.Name) --Gets an error here.
	end
end)

Any help would be appreciated!

1 answer

✓ Accepted answer

You're getting the error "Unable to cast value to Object" because you're trying to pass a string. Also, the first argument needs to be the player you're invoking, then whatever you want to send.

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent then 
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			-- Player you're invoking, then the model. 
			Radio:FireClient(player, hit.Parent) 
			-- Other Example: Radio:FireClient(player, 'hi') 
		end
	end
end)

The Response in your LocalScript would be

Radio.OnClientEvent:connect(function(object)
	print(object) -- Player1
	-- Other Example prints 'hi'
end)

1 comment

0

Thank you!

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