Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to change the value of something when it touches a block? [UN-SOLVED]

So I've made a boat, and in the boats hull theres a BodyForce instance with Y axis set to 26500. Im trying to make this reduce to 13000 when the boat either touches a block or the Terrain, I tried doing terrain in general, but that wouldnt work as Im using roblox water for the sea. I needed it to only work when it touched the Grassy block from default roblox terrain...

I tried making a script that was in the ships hull, but I actually forgot to save the game, so cant provide that. I'm not the best scripter although did watch some of the videos provided by roblox. If someone could give me a push in the right direction, would be much appreciated. Thank you in advance! =)

2 answers

Rays are able to return terrain and Parts

Try using this function that I made:

function CastRay(Object,Distance)
	local ray = Ray.new(
		Object.Position,
		Object.CFrame.lookVector*Distance)

local Hit = game.Workspace:FindPartOnRay(ray)
		return Hit
	end

BoatHull = game.Workspace.BoatHull--you dont need this
BoatHull.FrontSurface= "Motor" -- The motor represents where the ray is coming out from(you don't need this part)

while wait() do
--Example:
local Ray = CastRay(BoatHull,1)
	print(Ray)
-------------
	end

If you want it to work like the touch event make sure the distance is set to 1 like I did.

Have any questions about rays or anything else feel free to ask me.

Want to learn about Rays read these pages:

http://wiki.roblox.com/index.php/Raycasting

http://wiki.roblox.com/index.php?title=Scripting_Book/Chapter_17

2 comments

0

I appreciate your post, But looking at it that just detects if its hit something, not seeing what its hit then decreasing the BodyForce instance, How would that be implemented into this? Usually I would know but quite new to scripting and dont really understand most of that :)

1

It does see whatever the object is hitting in very low distance imitating the touch event and I'm also new to scripting. :P

I'll give you the start but not the whole thing because you could at least make an effort to make a script.

 game.Workspace.Boatpart.Touched:connect(function(Hit)
   end)

That's what I believe to be the start of that but I could be wrong, I'm a beginner scripter.

11 comments

0

That's the exact thing I done when I tried my self that didn't work but couldn't provide because the save didn't work. Thanks anyway

0

Yeah..I guess. Sorry man I'd help you out if I could but I don't know that much myself. You also need to add end in there somewhere

0

Hehe, No worries. Thanks for trying :)

0

Wait wait I apologize...have you tried (function(Hit) ?

0

Nope, what would that do?

0

it would do the same thing as (function(Touched) only i believe touched is wrong its Hit

0

(Touched) and (Hit) wouldn't matter, as they are just arguments. You could name it as (function (jksdhfjksdhf)... end) or something, lol.

0

":OnTouch()" is not correct, as it implies that it is a method, not an event. The anonymous function should start like this: game.Workspace.BoatPart.Touched:connect(function (hit) ... end)

0

(Hit) shouldn't be spaced from (function .. I think

0

Well, if it worked for you without spaces and it worked for me with a space, then I guess it works both ways.

0

;)

This discussion is preserved as it was. New answers, comments, and votes are closed.