Hi,
This is my very first post so I probably will make some mistakes
I have been making this game called ‘Tsunami Survival!’. In this game you need to build shelter on your own plot. The building system is nearly the same as in Build A Boat For Treasure: you build something, the tsunami comes after you click on a button… But now my problem comes: when a player for example builds a wall, the tsunami will still kill the player if he stands behind it. So I’m looking for someone who can give me tips on how to get along with this.
The script that’s in the tsunami part that kills the player:
function onTouched(Block)
if Block.Anchored == false then
Block:Remove()
end
end
script.Parent.Touched:Connect(onTouched)
Can anyone help me with this? If you need additional information, don’t be afraid to ask me!
5 Likes
I don’t know how but REPLACE REMOVE WITH DESTROY. Remove is deprecated and sets the part’s parent to nil and doesn’t free up any memory. This would turn into a nasty memory leak
3 Likes
you can use raycast in this situation
1 Like
What I would do is using a raycast when the player gets hit by the tsunami to check if there is anything in the opposite direction the tsunami is going.(Not including the tsunami). To not include the tsunami you can use RaycastParams to filter it out.
As other users already mentioned, Raycasting would be a good solution.
This should be the solution, only things that have to be done are giving the tsunami a path and casting the ray properly.
Tsunami Code
local tsunami = -- path to your tsunami object
function onTouched(block)
local hum = block.Parent:FindFirstChildOfClass("Humanoid")
if hum and hum.Health > 0 then
local head = hum.Parent:FindFirstChild("Head")
if head then
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {tsunami}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local result = -- launch a raycast going against the direction the tsunami is going
if result and result.Instance and result.Distance then
local raycastDistance = result.Distance -- get distance between ray origin and in
-- check if the player is standing right behind the wall by checking the raycast distance
if raycastDistance <= 10 then -- 10 studs, customise however you want
-- now that we know the player is standing behind a wall, we will "leave him alone"
return
end
end
end
else
if block.Anchored == false then
block:Destroy()
end
end
end
script.Parent.Touched:Connect(onTouched)
If you need source to read, here’s Robloxs’ official documentation regarding Raycasting.
Raycasting | Documentation
1 Like
So I’ve nearly filled in everything I could, but I can’t seem to calculate the direction of the ray.
As I’ve just learned the Raydirection can be calculated by ‘position of destination - position of origin’.
What is the position of destination? And is the humanoid the position of origin or is it the tsunami?
local tsunami = script.Parent
function onTouched(block)
local hum = block.Parent:FindFirstChildOfClass("Humanoid")
if hum and hum.Health > 0 then
local head = hum.Parent:FindFirstChild("Head")
if head then
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {tsunami}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local result = workspace:Raycast(tsunami.Position, ) -- launch a raycast going against the direction the tsunami is going
if result and result.Instance and result.Distance then
local raycastDistance = result.Distance -- get distance between ray origin and in
-- check if the player is standing right behind the wall by checking the raycast distance
if raycastDistance <= 10 then -- 10 studs, customise however you want
-- now that we know the player is standing behind a wall, we will "leave him alone"
return
end
end
end
else
if block.Anchored == false then
block:Destroy()
end
end
end
script.Parent.Touched:Connect(onTouched)
1 Like
So basically you the origin of the ray will be head.Position. I thought I would use the head because if we use the head we ensure that the character is not bigger then the obstacle it’s standing behind.
So your rayOrigin is head.Position. Now, shoot a straight ray which is for example 100 studs long in the direction the tsunami came from (not where the tsunami is going). Don’t forget to also include the raycastParams at the very end, seperated with a coma.
1 Like
First off, sorry for asking so much questions but I really want this game finished.
I’ve filled in everything that was needed including the rayparams, the direction and the origin of the ray, put it into a loop using ‘while true do’ but when I stand behind the wall I’m still being killed… Did I miss something?
local tsunami = script.Parent
local b = workspace.EndPoint
while true do
function onTouched(block)
local hum = block.Parent:FindFirstChildOfClass(“Humanoid”)
if hum and hum.Health > 0 then
local head = hum.Parent:FindFirstChild(“Head”)
if head then
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {tsunami}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local result = workspace:Raycast(head.Position, b.Position - head.Position, raycastParams) -- launch a raycast going against the direction the tsunami is going
if result and result.Instance and result.Distance then
local raycastDistance = result.Distance -- get distance between ray origin and in
-- check if the player is standing right behind the wall by checking the raycast distance
if raycastDistance <= 10 then -- 10 studs, customise however you want
-- now that we know the player is standing behind a wall, we will "leave him alone"
return
end
end
end
else
if block.Anchored == false then
block:Destroy()
end
end
end
end
script.Parent.Touched:Connect(onTouched)
Check the output for error and troubleshoot. For example when checking the distance between the part intercepted by the ray and the character print “player behind wall” for example. If your then behind a wall and this doesn’t print, something does not go as intended