As the title states I’m currently looking for some help with math.random property. I’m currently working on a boss fight in one of my games and at certain health points they will teleport around. This is working fine but the problem is the boss teleports to the same point every time making it predictable for the player on future attempts. I tried adding a math.random value to the script with three different locations but i’m honestly quite unfamiliar with the math.random property and have been unsuccessful implementing. I’ve included the script that is currently working well and good and I’ve also included the one I’ve been tinkering around with trying to add math.random in. If anyone could point me in the right direction i’d appreciate it.
while wait() do ----- Waits until Monster is near health threshhold
if script.Parent.Monster.Health <= 750 then --- Monsters health is at the threshhold now...
wait(1) ----- Wait a second
script.parent.HumanoidRootPart.CFrame = CFrame.new(-1846.2, 260.6, 3609.7)
script:Destroy()--- Teleports Monster to this location
end
end
Below is my edits to the script to try and make it decide between the three locations via math.random.
local location = math.random(1,3)
while wait() do ----- Waits until Monster is near health threshhold
if script.Parent.Monster.Health <= 750 then --- Monsters health is at the threshhold now...
wait(1) ----- Wait a second
script.parent.HumanoidRootPart.CFrame = location
if location == 1 then CFrame.new(-1846.2, 260.6, 3609.7)
elseif location == 2 then CFrame.new(-1575.379, 259.365, 3527.686)
elseif location == 3 then CFrame.new(-1575.379, 259.365, 3527.686)
end
end
end
It is inevitable that everytime he’ll be redirected to the same location yet again because your location variable is not changing every time his health is hitting the relocation point but rather staying frozen
Consider setting location to another randomly generated value under the health check
I think I get what you’re doing hopefully this works?
local _locationDict = {
[1] = Vector3.new(-1846.2, 260.6, 3609.7),
[2] = Vector3.new(-1575.379, 259.365, 3527.686),
[3] = Vector3.new(-1575.379, 259.365, 3527.686)
}
local function location(): number
return Random.new():NextInteger(1, #_locationDict)
end
local healthThreshold;
healthThreshold = script.Parent.Monster:GetPropertyChangedSignal("Health"):Connect(function() -- Instead of using a while loop this detects when the health changes
if script.Parent.Monster.Health <= 750 then --- Monsters health is at the threshold now...
wait(1) -- Wait a second
script.Parent:MoveTo(_locationDict[location()]) -- Teleport
-- If you want to destroy the script after it teleports the monster then uncomment this
--[[
-- Prevents memory leaks
healthThreshold:Disconnect()
healthThreshold = nil
-- Destroys the script
script:Destroy()
]]
end
end)
You need to move this line to the inside of the while loop, or even better, the inside of the if statement. What you are doing is getting a random number and assigning it to location. You only set it once, outside of the loop, and it won’t ever change.
You are setting the “location” once at the start which means it will be the same. Try setting the “location” inside the loop right before you teleport the boss. Also you can use an array with all the locations.
local location
local locations = { -- All the locations
CFrame.new(-1846.2, 260.6, 3609.7),
CFrame.new(-1575.379, 259.365, 3527.686),
CFrame.new(-1575.379, 259.365, 3527.686)
}
while wait() do ----- Waits until Monster is near health threshhold
if script.Parent.Monster.Health <= 750 then --- Monsters health is at the threshhold now...
wait(1) ----- Wait a second
location = locations[math.random(1, #locations)] -- Sets the location to a random item from the list
script.parent.HumanoidRootPart.CFrame = location
end
end
Hey this was a nearly perfect solution and was exactly what i was looking to accomplish, i just had to add a script:destroy() at the end to prevent the boss from teleporting over and over. Thanks! And for anyone curious about the finished script I’ve included it below
local location
local locations = { -- All the locations
CFrame.new(-1846.2, 260.6, 3609.7),
CFrame.new(-1575.379, 259.365, 3527.686),
CFrame.new(-1575.379, 259.365, 3527.686)
}
while wait() do ----- Waits until Monster is near health threshhold
if script.Parent.Monster.Health <= 750 then --- Monsters health is at the threshhold now...
wait(1) ----- Wait a second
location = locations[math.random(1, #locations)] -- Sets the location to a random item from the list
script.parent.HumanoidRootPart.CFrame = location
script:Destroy()
end
end