Elseif thinks the value is awlays true

ok so, I made a script that checks conditions in an order and one of them is always true??

wait(15)
local h = workspace.bed.pillioe.hungry.Value
local l = workspace.bed.pillioe.light.Value
local p = workspace.bed.pillioe.pill.Value
workspace.bed.pillioe.ClickDetector.MouseClick:Connect(function()
	if h == true then
		script.Parent.hungry.Visible = true
		wait(3)
		script.Parent.hungry.Visible = false
	elseif p == true then
		script.Parent.PILLS.Visible = true
		wait(3)
		script.Parent.PILLS.Visible = false
	elseif l == true then
		script.Parent.LIGHT.Visible = true
		wait(3)
		script.Parent.LIGHT.Visible = false
	else 
		script.Parent.Parent.ScreenGui.Frame.Transparency = 0
	end
end)

it always thinks p == true even if I make it false, p is a boolean value
(pillioe is a typo of pillow)
the script is in a screengui in startergui and I want it to make the right gui visible but it always makes the PILLS gui visible
(don’t ask about the game, just help me please)

The above part saves the current values into the variables, and doesn’t store the path.

Instead, define the path and access the value by indexing it with .Value.

local valueBase = workspace.BoolValue
valueBase.Value --> true/false

EDIT.

A friendly suggestion: Store all those script.Parent paths into separate variables at the top. Easier to read, access, and make sure everything is properly loaded, e.g.

local light = script.Parent:WaitForChild("Light")

EDIT 2.

@ANGONGUS_NOOB

I’d like to anyway address some code practices and append the proper approach. This is probably the best one can do without knowing the deeper context of your script, other scripts and intentions. I kept the original naming.

-- Avoid using 'wait' to solve problems with loading.
local hungry = workspace.bed.pillioe.hungry
local light = workspace.bed.pillioe.light
local pill = workspace.bed.pillioe.pill

local hungryFrame = script.Parent:WaitForChild("hungry")
local lightFrame = script.Parent:WaitForChild("LIGHT")
local pillsFrame = script.Parent:WaitForChild("PILLS")

local frame = script.Parent.Parent:WaitForChild("ScreenGui"):WaitForChild("Frame")

workspace.bed.pillioe.ClickDetector.MouseClick:Connect(function()
	if hungry.Value then
		hungryFrame.Visible = true; task.wait(3); hungryFrame.Visible = false
	elseif light.Value then
		lightFrame.Visible = true; task.wait(3); lightFrame.Visible = false
	elseif pill.Value then
		pillsFrame.Visible = true; task.wait(3); pillsFrame.Visible = false
	else
		frame.Transparency = 0 -- not sure why this part, but I kept it.
	end
end)
2 Likes

Hey there, your issue might be, that the values aren’t changing whenever the ClickDetector is clicked. To solve this, you either put the 3 variables in a function, so whenever it fires their value changes. Or you can always call the value.

This would be solution one:

local h
local l
local p
workspace.bed.pillioe.ClickDetector.MouseClick:Connect(function()
	h = workspace.bed.pillioe.hungry.Value
        l = workspace.bed.pillioe.light.Value
       p = workspace.bed.pillioe.pill.Value
        if h == true then
		script.Parent.hungry.Visible = true
		wait(3)
		script.Parent.hungry.Visible = false
	elseif p == true then
		script.Parent.PILLS.Visible = true
		wait(3)
		script.Parent.PILLS.Visible = false
	elseif l == true then
		script.Parent.LIGHT.Visible = true
		wait(3)
		script.Parent.LIGHT.Visible = false
	else 
		script.Parent.Parent.ScreenGui.Frame.Transparency = 0
	end
end)```

I hope this helps and solves your problem!
1 Like

Try this.

wait(15)
local h = workspace.bed.pillioe.hungry
local l = workspace.bed.pillioe.light
local p = workspace.bed.pillioe.pill
workspace.bed.pillioe.ClickDetector.MouseClick:Connect(function()
	if h.Value == true then
		script.Parent.hungry.Visible = true
		wait(3)
		script.Parent.hungry.Visible = false
	elseif p.Value == true then
		script.Parent.PILLS.Visible = true
		wait(3)
		script.Parent.PILLS.Visible = false
	elseif l.Value == true then
		script.Parent.LIGHT.Visible = true
		wait(3)
		script.Parent.LIGHT.Visible = false
	else 
		script.Parent.Parent.ScreenGui.Frame.Transparency = 0
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.