Jump to content

Chance of talking when in radius of an entity


Recommended Posts

My character has mottephobia, AKA fear of butterflies and moths! I have it so they lose sanity when in a radius of a butterfly, but I wanted there to be a chance of them announcing when they get near a butterfly. I got everything right, except I probably don't have the math.random() done right, because when the chance < 0.50, they never say it. But when I change it to chance < 0.75, they say it every time. Here's the code.

	local chance = math.random()
--near butterfly
	inst:DoPeriodicTask(
            0.1,
            function(inst)
                local x, y, z = inst.Transform:GetWorldPosition()
                local ents = TheSim:FindEntities(x, y, z, 4.0, {"butterfly"})
                if #ents > 0
                then
                    if inst.wisniowski_mottephobia == nil
                    then
                        

						if chance < 0.50 then
							inst.components.talker:Say(GetString(inst.prefab, "ANNOUNCE_BUTTERFLY"))
							inst.components.sanity.dapperness = (TUNING.DAPPERNESS_LARGE * -1)
							inst.wisniowski_mottephobia = true
						else
							inst.components.sanity.dapperness = (TUNING.DAPPERNESS_LARGE * -1)
							inst.wisniowski_mottephobia = true
						end

						
                    end
                else
                    if inst.wisniowski_mottephobia ~= nil
                    then
                    	inst.components.sanity.dapperness = (TUNING.DAPPERNESS_SMALL * -1)
						inst.wisniowski_mottephobia = nil
                    end
                end
            end
    	)
	

Any help is appreciated!

Edited by Cherryzion
Link to comment
Share on other sites

replace "chance < 0.50" with "math.random() < 0.50" in your code (and remove local chance...above)

The way it is at the moment chance is set one time, so it will be always eg. 0.3, without changing. The result is always your text or never.

Edited by Serpens
Link to comment
Share on other sites

12 minutes ago, Serpens said:

replace "chance < 0.50" with "math.random() < 0.50" in your code (and remove local chance...above)

The way it is at the moment chance is set one time, so it will be always eg. 0.3, without changing. The result is always your text or never.

Works! Thanks a ton!

Link to comment
Share on other sites

10 hours ago, SuperDavid said:

Wouldn't calling this code every .1 seconds cause lag or something? Just wonderin' :)...

From what I can tell, it doesn't. All it's doing anyway is adding a couple lines of code. But if you know another way that undeniably is impossible to cause lag, I could try it out!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...