How to prevent BindableEvent from not running?

I have a script that loads in models (trees) when the game is loaded. However, the code is unreliable, as sometimes the game is slow to load and the BindableEvent doesn’t fire, causing the game to not load any trees. Is there a way to make the game wait until at least the point where Roblox stops showing the loading screen to fire the event?

Hmm, I think this would be a perfect case for :WaitForChild()

What it basically do is yield the code until it finds the child it’s looking for. It has 2 parameters. the string and the timeout

string: The name of the child it’ll wait for.
timeout (optional): The amount of time in seconds before it times out.

With this, you can wait for the BindableEvent to load before firing it. Make sure to also wait for the script that will receive the event from that BindableEvent, as the connection needs to be made earlier before firing the event.

Example:

local Bindable = game:GetService("ReplicatedStorage"):WaitForChild("BindableEvent")

game:GetService("ServerScriptService"):WaitForChild("BindableEventReceiverScript")

Bindable:Fire()

Hopefully you find this helpful!

I tried using this method, but still nothing happens. Here’s the code if it helps:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetDataStore("NewWorld")
local RS = game:GetService("ReplicatedStorage")

local TreeLoad = game:GetService("ReplicatedStorage").Data:WaitForChild("LoadTrees")

local savedValue = true

Players.PlayerAdded:Connect(function(player)
	if savedValue == true then
		savedValue = DataStore:GetAsync("NewWorld")
		--savedValue = false
		if savedValue == true then
			print("First time loading up a save")
			savedValue = false
			DataStore:SetAsync("NewWorld", savedValue)
			
			RS.Data.NewTrees:Fire()
			
			
		
		else
			warn("Loading existing data")
			game:GetService("ServerScriptService").DataStores:WaitForChild("Trees")
		
			TreeLoad:Fire()
		end
	end
	if savedValue == true then
		game.ServerStorage.FirstTimeLoading.Value = true
	else
		game.ServerStorage.FirstTimeLoading.Value = false
	end
end)

Players.PlayerRemoving:Connect(function(player)
	warn(game.ServerStorage.FirstTimeLoading.Value)
	game.ServerStorage.FirstTimeLoading.Value = savedValue
end)

Are you making sure to wait for scripts to load before firing the bindable?

Consider that event connection is the top priority, you can either wait for that script to load, or make that script communicate with others when its loaded.

So do I need a separate script to wait for that script to load fully, then enable it?

Well, what I ment is that the loading script will be yielded until it receives an event from a script that receives tree loading event. This way, you can make sure that all scripts are connected in time before firing events.

Or just add task.wait() after waiting for the tree loading script.

Your original idea worked. It was a stupid mistake on my end. I found a wait(3) before the tree functions in another script would start to listen for the event.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.