#Is it possible to make it check if the player is in range twice when attacking?

120 messages · Page 1 of 1 (latest)

next valve
#

I want it so that when the player gets in range intially, the monster does their attack animation, but the player has a second to dodge before actually taking damage. This scipt however makes the player still take damage even if they've ran far enough away

cedar osprey
#

run the check twice

#
local function checkWithinRange()
  local myPos, targetPos = Monster:GetCFrame().p, Monster:GetTargetCFrame().p
  return (myPos-targetPos).Magnitude
end

if checkWithinRange() <= value then
  ...
  if checkWithinRange() <= value then
    ...
  end
end

something like that

#

a less clean way to do what you want to do would be running

if  (Monster:GetCFrame().p - Monster:GetTargetCFrame().p)  <= .... then ... end

twice

brisk sundial
gilded halo
cedar osprey
#

this is not it

next valve
next valve
#

because it underlines red

cedar osprey
cedar osprey
next valve
#

i dont know scripting that well

#

Would it look something like this?

next valve
cedar osprey
#

You have to incorperate the conditional if checkWithinRange() <= value check into the Attack function

tidal forgeBOT
#

studio** You are now Level 12! **studio

cedar osprey
#

also don't use local function like I showed in the example, use function since I didn't see it was a module script

#

<= - if value1 is less or equal to value2
>= if value1 is greater or equal to value2

#
function Monster:CheckWithinRange(Monster)
  local myPos, targetPos = Monster:GetCFrame().p, Monster:GetTargetCFrame().p
  return (myPos-targetPos).Magnitude --// gets the vector length between your position and the targets position
end
function Monster:Attack(Monster)
  if self:CheckWithinRange(Monster) <= value then
    ...
    if self:CheckWithinRange(Monster) <= value then
    ...
    end
  end
end
#

You might want to replace Monster:GetCFrame().p with a player element

#

since It's getting the CFrame of the object Monster twice instead of getting the CFrame of the Player and Monster

#

unless that's what you're intending to do

next valve
#

It is supposed to attack the player

#

So I'm assuming that means it needs to be a player element

#

I think this may work

#

It is damaging me only when I'm extremly close

next valve
cedar osprey
#

Replace the Monster within tbe range function with NPC or something else since the module itaelf is called Monster already

#

Also you want to get the distance between the player and the monster, to di that you need to change the Monster witih the myPos variable with the Player

next valve
#

Which one of these do I change to monster?

#

I'm very confused I'm sorry I don't know what I'm doing

#

And by changing it to NPC do you mean Changing the Monster from Monster:CheckWithinRange cause im pretty sure that'll break the script as all other functions start with Monster:

next valve
#

I must've completely misunderstood you

cedar osprey
#

to simplify your code a bit for your understanding

function Monster:CheckWithinRange(Monster)
  local myPos = Monster:GetCFrame().p --// gets the Monsters CFrame
  local targetPos = Monster:GetTargetCFrame().p --// Also gets the Monsters CFrame
  return (myPos-targetPos).Magnitude --// gets the vector length between your position and the targets position
end
#

Within the module script, your module is called Monster

#

The only parameter you're currently passing through to the CheckWithinRange function is Monster

#

The names will conflict

#

The purpose of the CheckWithinRange function is to check the distance between the player and the monster if I'm correct

#

currently you're creating a local variable called myPos and another one called targetPos, but they're the same think

#

you're trying to take away the same value as the value from the value

#

e.g.

100(monster position) - 100(monster position)

this will equal to 0 and that's why you only get damaged when you're extremely close (when your range is less than 1)

#

you have to change one of the variables to take in the current players CFrame instead of the monsters

#

by adding a new parameter and changing the myPos variable you can get the result you want

#
function Monster:CheckWithinRange(Current_Player: Player,Monster)
    local myPos = Current_Player:GetCFrame().p
    ....
end
#

I am assuming :GetTargetCFrame() is the same as :GetCFrame() since it's used for a NPC monster and no additional information is given to the function. This is the first time I've seen :GetTargetCFrame()

next valve
#

The script is not a module script btw

#

It's just a normal script

#

This is explained much better tho

#

I will have a look some time later

cedar osprey
#

ah, woudln't recommend using : inbetween things then, since that's usually done within module scripts and confuses things a bit

next valve
#

oh

#

I was just wondering

#

if ur up to it

#

do u think it would be beneficial for me to send the entire script?

#

This is what I have so far and it doesn't work

#

The monster runs towards where the player was standing when they first walked in range and then once the monster gets there it stops moving. It also never attacks

cedar osprey
#

since it's not a module script replace the self:checkwithinrange with just CheckWithinRange(Monster)

#

also I'd recommend using local function instead of global function like you're using

next valve
#

@cedar osprey now what do I put instead of current_player

#

to get the player's CFrame

#

This changes nothing with how the script works

#

it's still broken

#

how do I fix it?

#

it was working before but the range was just too small

#

can you just tell me how to make it work with a large range

cedar osprey
#

add print statements after each conditional check and check where the script stops working

#

or just skips

next valve
#

ok

next valve
#

This is the issue @cedar osprey

#

Line 323

#

It's cause the Current_Player doesn't exsist yet

#

It's hard to explain but I think I get it

#

Like here I changed it

#

It's getting the Player from the bracket's CFrame

#

But Player isn't anything

#

Like there's nothing telling what Player actually means

#

I think

#

U are the more knowledgable here

#

but there needs to be some way to actually detect the player who is being chased and make them the Player

next valve
#

about that

#

they're different

#

I think that also has something to do with why it isn't working

#

GetTargetCFrame is getting the CFrame of the monster's target/player it's chasing

#

I'll show u

tidal forgeBOT
#

studio** You are now Level 11! **studio

next valve
#

but we've been misunderstanding the code this whole time

next valve
#

cause how u get the player's CFrame is with GetTargetCFrame

#

This is the new error I'm getting with my new code @cedar osprey

next valve
#

The CheckWithinRange Works now just not a second time

#

im gonna try getting rid of Self:

#

Ok so that kinda worked

#

It's gone back to only attacking when you're extremly close to the monster now

next valve
#

This is what GetTargetCFrame does

#

If I just edit the attack range value on the second CheckWithinRange then I can make it work

#

This is actually working pretty well

#

I think I'm happy with it

cedar osprey
cedar osprey
next valve
#

Yeah

#

It took so long just to get here

#

So ye

#

I think I'll keep it

#

Thank for all your help though

next valve
#

not sure if you can help with this one as it's not as scripting-based

#

but I'm having a completely new issue with my monsters now

#

the last thing I want to add to them before they're complete is a death animation

#

this is the thread if ur interested #1162588409534091314 message

#

all good if ur not tho

#

like I said it's less scripting based and more animation based

#

and u've helped me a lot already

#

but maybe since you know a bit about the monster models already I thought I'd ask

cedar osprey
#

unfortunately I haven't done anything much with animating as a whole so I won't be able to help you out with that