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

Function doesn't do anything even though remote event is received ?

Asked by
kylaex 17
3 years ago
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StadSwitchEvent = ReplicatedStorage:WaitForChild("StadSwitch")
local DeleteStadAndAdd = workspace.StadiumSelect

local function OnStadSwitchEventFired()
    print("i dont work")
    for i,v in pairs(DeleteStadAndAdd:GetChildren()) do
        if v:IsA("Model") then
            v:Destroy()
        end
    end
end

StadSwitchEvent.OnClientEvent:Connect(OnStadSwitchEventFired) 

How would I fix this?

0
Any errors? Is the print statement working? Maximus1027 30 — 3y
0
if the print works then there's probably something wrong with your loop, try using ipairs instead. CjayPlyz 643 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

There's a few reasons why your script might not work. The first potential reason is the second line of your script:

local StadSwitchEvent = ReplicatedStorage:WaitForChild("StadSwitch")

Calling the WaitForChild method without the second argument can result in an infinite yield which is when the script is paused forever. If you manually created the RemoteEvent under ReplicatedStorage, then this shouldn't be a problem. However, if you created the RemoteEvent using a server script, you need to make sure that the RemoteEvent is actually being created with the correct parent (ReplicatedStorage in this case) and the correct name (StadSwitch in this case).

The second potential issue is the third line of the script:

local DeleteStadAndAdd = workspace.StadiumSelect

Unless you're confident that this instance is always part of the workspace and isn't replaced at any time, you might want to use FindFirstChild to prevent errors. If you do this, make sure you check if the value of the variable is nil so you can prevent errors later on in the script.

I've looked at your script and chances are that your code works fine which leads me into the third potential issue. You're probably just calling the RemoteEvent incorrectly from the server. To trigger the OnClientEvent event for a specific player, use the FireClient method on the server:

-- assume StadSwitchEvent is the RemoteEvent referenced in LocalScript
-- also this is a server script not a local script
StadSwitchEvent:FireClient(game.Players["NAME"])

If you want to trigger the event for every player in the game at the same time, you can use the FireAllClients event:

-- assume StadSwitchEvent is the RemoteEvent referenced in LocalScript
-- also this is a server script not a local script
StadSwitchEvent:FireAllClients()

Because your RemoteEvent doesn't accept any arguments, none were provided in the examples above, but if your RemoteEvent accepts arguments, then you just pass them after the player argument for FireClient or just pass them directly for FireAllClients.

I hope this helps you find the solution to your scripting issue :)

Ad

Answer this question