How would I use RayCast to replace .Touch events for Tycoons Buttons?

Hello,

I have a few posts about an issue I have been having in a tycoon system I have, where simple touches on the button or the cash collector won’t register. However, if you were to reset on any of the buttons in this scenario, and all your body parts touch said button, the .Touched event registers, and you can collect your cash or buy said button. I have more about this issue in this post.

I’m not exactly sure why this is happening, especially given how I have not been able to replicate it myself, as it does not happen regularly whatsoever. I don’t know if this some sort of issue with the afflicted player’s networking, something to do with ROBLOX’s .Touched events, or something on my end, but I need alternatives to avoid this bug from existing, even though it is rare

The first alternative I was gonna use was convert everything to ProximityPrompts, since I already use those for a few things in my game and they have no issues, but the game’s buttons and cash collectors have been built/designed/set up around them being .Touched events.

I am looking for help on how I would approach converting all the .Touched events of all the buttons in the tycoons and the cash collector to be RayCast based. I haven’t used RayCasts for anything like this before, so I have no idea where to start. How would I begin to start? Where would I place the detection for when the RayCast detects that the owner of the tycoon comes in contact with the button(s) or cash collector? What about running too many RayCasts in a server where there could be 8-16 players who have dozens of buttons to buy at a time each, meaning there could be hundreds of RayCasts running?

1 Like

I found this recent bug report which may be related to your Touch event issue:

It seems that Touched events established on the server are failing to react to client-owned parts (characters are client-owned by their players). So as a temporary fix you could have the Touched event handled on the client through a LocalScript and use a RemoteEvent to communicate to the server.


However if you are still interested in a RayCast method instead I can provide a basic template to help you get started:

-- Use a whitelist so the ray will only interact with the tycoon buttons
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = -- Place a table of all THIS player's tycoon's buttons here

-- 'origin' is where the ray starts, 'travel' is how it moves (direction & length)
local origin = character.HumanoidRootPart.Position
local travel = -Vector3.yAxis * 5 -- Move downwards, assuming your buttons are below the player on the ground

local raycastResult = workspace:Raycast(origin, travel, raycastParams)
if raycastResult then
	-- TODO The raycast hit a tycoon button, do tycoon things
	local button =  raycastResult.Instance -- The button hit
end

That should allow you to do a raycast for a single player, you’ll have to implement it to work for all players yourself.

Raycasts aren’t expensive, doing ~16 raycasts with a small length of 5 every frame isn’t going to freeze your game or anything. But if you’re worried about it you could make it so raycasts are only done once every x seconds.

1 Like

Oh wow, that would explain why this issue has been completely random. I wish I knew it was a ROBLOX bug yesterday and the day before, then I wouldn’t have wasted so much of my Friday night and my sanity trying to fix it.

I think I might just temporarily convert everything over to use ProximityPrompts instead perhaps, but I will fiddle around with using RemoteEvents for the time being. I may just put a RemoteEvent in each button that fires when it is touched - would that be bad practice?

No, that should be fine to do. Just make sure the client is only telling the server that the button was touched and the server should determine how much money they deserve. It would be bad practice to tell the server the player deserve $1000 as anything sent from the client can’t be trusted.

1 Like

Yeah no, I wouldn’t be doing that, lol.

Thanks for the help. I really wish I knew this was a ROBLOX bug sooner - in that thread you showed, it seems like a lot of huge games are having the same issue

I saw someone say in the thread you linked, that even .Touched events on the client are broken in some instances.

I think I will be temporarily switching to ProximityPrompts for the time being.