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

Problems With BindableEvents and WaitForChild(), fix?

Asked by
KroKey 0
4 years ago
Edited by DeceptiveCaster 4 years ago

Hello. So I was learning about BindableEvents and I was trying to fire a BindableFunction for it to print something to the output. The problem comes when I either run or play and when I do either it sometimes prints to the output and sometimes doesn't, I believe that the script may run before the BindableEvent even loads, and this is what WaitForChild() is for, but it looks like that it is not waiting for the BindableEvent and that sometimes it works and sometimes doesn't.

This is the script that fires the BindableEvent:


PrintStuff = game.Workspace:WaitForChild("PrintStuff") PrintStuff:Fire()

This is the scripts that picks up the BindableEvent:


PrintStuff = game.Workspace:WaitForChild("PrintStuff") function printSomething() print("Stuff") end PrintStuff.Event:Connect(printSomething)

This is an image of the Explorer folder in case it is needed:

http://prntscr.com/nyvpno

The expected output would be that the BindableEvent would fire and print "Stuff" every time I either run or play.

0
You arn't defining the variables correctly, use local. H_exadecimal 9 — 4y
0
It looks like that using local makes the failure rate just drop but not disappear. (i put local before the PrintStuff variables and printSomething function) KroKey 0 — 4y
0
Try waiting a few seconds before you fire the event in the first script. AIasdair 50 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Try waiting a few seconds before you fire. You shouldn't need to do this in most cases, but since you're testing it when both scripts load at the same time it is necessary.

PrintStuff = game.Workspace:WaitForChild("PrintStuff")

function printSomething()
    print("Stuff")
end

wait (10)

PrintStuff.Event:Connect(printSomething)
0
Thanks. Is there any possible way to fire the remote as soon as it gets added? KroKey 0 — 4y
Ad

Answer this question