I’m scripting some models in my game to break when shot/blown up, but I’m struggling to figure out the best way to do so
at first I lazily put a couple if statements in my raycast code but I knew I needed to do something better. would it be ok to put scripts in each model that only run when an attribute is changed? I’ve thought about this but it doesnt sound very awesome sauce having tons of scripts in every model
Im thinking about creating a single script that will handle all of the props with boolean attributes just dunno how I’ll get that working
Use a ModuleScript.
When your Raycast detects a destructible prop, it will require that ModuleScript, passing that destructible prop along so the ModuleScript can modify it.
Example:
Main Script
local PropDestructor = require(script.ModuleScript)
-- follow up with your code here..
-- now here's what would execute once your raycast connects and it detects a breakable prop
PropDestructor.DestroyProp(PropModel)
Module Script
local module = {}
function module.PropDestructor(PropModel)
-- functionality for prop destruction
end
return module
If the raycast is done on the client, you will need to fire it to the server through a RemoteEvent so it’ll be replicated to everyone.
Pro coder tip: When possible, put all your scripts in ServerScriptService.
I know how modulescripts work and everything my problem is getting everything connected since different regions in my map regenerate at different times sort of like the old crossroads map so i have to figure out how to connect every prop to the script
That’s how. Your Raycast will interact with that prop, and will pass that over to the ModuleScript which will do everything else. No need to hard code everything.
You’re welcome. Just remember to pass it through a RemoteEvent so the code is executed on the server, that is, if your Raycast is being created on the client.
Otherwise, the changes will not replicate to everyone else.