Can't call BindableEvent inside a RayHit because of sandboxing of thread

  1. I want to call the HitMarker BindableEvent inside of RayHit event to simulate the bullet hitting.

  2. What is the issue?

  3. What solutions have you tried so far?
    Tried wrapping the event inside a task.defer(), yet it still shows the same error.

-- This is the codeblock
local function visualizeBulletImpact(
	toolSettings: {},
	toolEffects: {},
	hit: Instance,
	position: Vector3,
	surfaceCFrame: CFrame,
	normal: Vector3
)
	local target = hit.Parent
	
	local humanoid, character = InstanceHelper:FindHumanoid(hit)
	
	if not humanoid then
		if toolSettings.BulletHoleEnabled then
			local Hole = Instance.new("Part")
			Hole.Name = "BulletHole"
			Hole.Transparency = 1
			Hole.Anchored = true
			Hole.CanCollide = false
			Hole.Size = Vector3.new(1, 1, 0.2)
			Hole.TopSurface = 0
			Hole.BottomSurface = 0

			local Mesh = Instance.new("BlockMesh")
			Mesh.Offset = Vector3.new(0, 0, 0)
			Mesh.Scale = Vector3.new(toolSettings.BulletHoleSize, toolSettings.BulletHoleSize, 0)
			Mesh.Parent = Hole

			local Decal = Instance.new("Decal")
			Decal.Face = Enum.NormalId.Front
			Decal.Texture = "rbxassetid://"..toolSettings.BulletHoleTexture[math.random(1,#toolSettings.BulletHoleTexture)]

			if toolSettings.PartColor then
				Decal.Color3 = hit.Color
			end

			Decal.Parent = Hole
			Hole.Parent = workspace.CurrentCamera
			Hole.CFrame = surfaceCFrame * CFrame.Angles(0, 0, math.random(0, 360))

			if not hit.Anchored then
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = Hole
				weld.Part1 = hit
				Hole.Anchored = false
			end

			-- Fade
			if toolSettings.BulletHoleFadeTime then
				if toolSettings.BulletHoleFadeTime > 0 then
					task.spawn(function()
						task.wait(toolSettings.BulletHoleVisibleTime / 2 or 2)
						local lastTick = tick()
						while true do
							local alpha = math.min(tick() - lastTick / toolSettings.BulletHoleFadeTime, 1)
							Decal.Transparency = LerpHelper:numLerp(0, 1, alpha)
							if alpha == 1 then break end
							RunService.Heartbeat:Wait()
						end
					end)
				end
			end

			Debris:AddItem(Hole, toolSettings.BulletHoleVisibleTime or 5)
		end
	end
	
	-- HIT MARKER
	if toolSettings.HitmarkerEnabled then
		local isHeadshot = InstanceHelper:IsHeadshot(hit)
		HitMarker:Fire(isHeadshot)
	end
end

Caster.RayHit:Connect(function(activeCast, result: RaycastResult, velocity: Vector3, cosmeticBullet: Instance)
	local hit = result.Instance
	local toolSettings = activeCast.UserData.ToolSettings
	local toolEffects = activeCast.UserData.ToolEffects
	local position = result.Position
	local normal = result.Normal
	local surfaceCFrame = CFrame.new(position, position + normal)
	visualizeBulletImpact(toolSettings, toolEffects, hit, position, surfaceCFrame, normal)
end)

I’m using the FastCastRedux api and the AI recommends me to modify the Signal module inside of it, and I think its a bit hassle to edit the internal script and it may break future implementations.

Setting the Sandboxed to true made it work. Though, security wise its not recommended.

Since its only a BindableEvent, only the client can only use and hear it.

I wonder what other way of fixing it other than forcing sandboxed.

Fixed code?:

local HitMarker = Remotes:WaitForChild("HitMarker") :: BindableEvent
HitMarker.Sandboxed = true


Caster.RayHit:Connect(function(...)
    -- Hit check
    HitMarker:Fire(...)
    ...
end)

No
Just set all models sandboxed to false
sandbox purely exists for running untrusted code

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