Really messy and broken piece of code

I got this code here, and It’s broken. It’s also hard to understand (for me at least), I’m not sure what I was thinking writing this, if anyone can help me fix it will be appreciated.

(eventtype.value and current.value are not being updated when the functions are ran)

(the while wait(2) do part is just a placeholder)

local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local events = 5

chosenEvent = ""
local ce = chosenEvent

local values = rep.Values
local current = values.CurrentEvent
local eventtype = current.EventType
local highlight = current.ObjectToHighlight

local function chooseEvent()
	chosenEvent = math.random(1, events)
end

local function executeEvent()
	if ce == 1 then
		eventtype.Value = "Player"
		current.Value = "A player will get +10 jump power"
	elseif ce == 2 then
		eventtype.Value = "Player"
		current.Value = "A player will become sparkly"
	elseif ce == 3 then
		eventtype.Value = "Arena"
		current.Value = "Arena: Fire will fall from the sky"
	elseif ce == 4 then
		eventtype.Value = "Plate"
		current.Value = "Someone's plate will light on fire"
	elseif ce == 5 then
		eventtype.Value = "Plate"
		current.Value = "Someone's plate will flip upside-down"
	end
end

while wait(2) do
	chooseEvent()
	executeEvent()
	
	print(ce, eventtype.Value, current.Value)
end
2 Likes

What is this even for? What are you trying to fix? Also what is the issue / what happens when you try to run it? Please be more specific or we can’t help you

eventtype.value and current.value are not being updated when the functions are ran, they are blank when printed

It’s a plates-of-fate themed game, this script will choose and execute events

I believe it’s because you are using the same name, current, for both a local variable and a property of the values object. You need to either use different names for different variables, or use the full path to access the property.

For example, you have this line:

local current = values.CurrentEvent

This means that current is a local variable that refers to the CurrentEvent property of the values object. However, later in your code, you have this line:

current.Value = “A player will get +10 jump power”

This means that you are trying to set the Value property of the local variable current, not the CurrentEvent property of the values object. This will not work, because the local variable current does not have a Value property. You need to either change the name of the local variable, or use the full path to access the property, like this:

values.CurrentEvent.Value = “A player will get +10 jump power”

The same applies to the eventtype variable and property. You need to make sure that you are using the correct names and paths for your variables and properties, otherwise they will not be updated correctly.

1 Like

Does showing you this change that explanation or no:

snip

The problem is that you are not updating the ce at all. It is always going to be “” unless you actually update it.

2 Likes

Edit: ah i see thanks

Summary

This text will be hidden

That’s the structure of the values object in the workspace, which contains the CurrentEvent property. However, in your code, you are using a local variable named current to refer to the CurrentEvent property. This is not the same as the property itself. You need to use the full path to access the property, like this:

values.CurrentEvent.Value = “A player will get +10 jump power”

Otherwise, you are just setting the value of a local variable that has nothing to do with the property.

1 Like

To fix this, all he has to do is change the one function to:

local function chooseEvent()
	ce = math.random(1, events)
end
1 Like

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