Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Mayk728 855 Moderation Voter
7 years ago

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

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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)
0
Thank you! Mayk728 855 — 7y
Ad

Answer this question