2 Mode Obby Problem

  1. What do you want to achieve? I am trying to make an obby that switches between day time and night time, with blocks in the stages appearing depending on what mode you’re in. (This needs to be done locally, by the way.)

  2. What is the issue? I am trying to make the parts for one mode lose collision and get the forcefield texture when the mode they’re for is not active, and gain collision along with their regular texture when the mode they’re for is active, but it won’t work and I’m not sure why.

  3. What solutions have you tried so far? I’ve tried looking on the dev forum for something related to this problem and trying to fix it myself (switching it between local and server script, editing the code and editing its parent in the game), but nothing has worked so far.

This is the code I’m currently using. It’s in a local script inside a part i want to be using for one of the modes.

local modeToggle = game.Players.LocalPlayer.PlayerScripts:WaitForChild("TimeSwitch"):WaitForChild("Day")

while task.wait(0.01) do
	if modeToggle.Value == true then
		script.Parent.Material = Enum.Material.Plastic
		script.Parent.CanCollide = true
	elseif modeToggle.Value == false then
		script.Parent.Material = Enum.Material.ForceField
		script.Parent.CanCollide = false
	end
end

I appreciate any help I can get with this. Please tell me if you need anything explained or need more info

1 Like

Issue 1: LocalScripts only run if they’re a descendant of a player or their character (or you can use a script and set its RunContext to Client [do this, its easier for now])

Issue 2: Please look into GetPropertyChangedSignal, I shouldn’t have to explain why while task.wait(0.01) do is bad

Is this closer to what I should be doing? (Aside from the script changes I also put the code into a server script with Client as its RunContext, though I didn’t change the location of the script, not sure if I’m supposed to or not.)

local modeToggle = game.Players.LocalPlayer.PlayerScripts:WaitForChild("TimeSwitch"):WaitForChild("Day")

modeToggle:GetPropertyChangedSignal("Value") do
	if modeToggle.Value == true then
		script.Parent.Material = Enum.Material.Plastic
		script.Parent.CanCollide = true
	elseif modeToggle.Value == false then
		script.Parent.Material = Enum.Material.ForceField
		script.Parent.CanCollide = false
	end
end

it’s better to use .Changed over :GetPropertyChangedSignal because it was designed for BaseValues

.Changed

1 Like

So are you saying something like modeToggle.Changed:Connect(function() would work better in that line?

yes. indeed it would work better

also its :GetPropertyChangedSignal("Value"):Connect(function()

for convenience, .Changed has a parameter of the new value.

.Changed:Connect(function(newvalue)

1 Like

To incorporate the newvalue property from the function into the script, would I do something like changing the if modeToggle.Value == true to if newvalue == true or is that not how it works? (I’ve never really used .Changed, sorry.)

1 Like

if newvalue == true then/if newvalue == false then
or
if newvalue then/if not newvalue then

these both work

So what I said would work then? If so, I appreciate the help, but my script is still not changing the block when I switch modes. (Also, if I try to use print in the script to check if it works, it isn’t printing anything. That info may be of use to you.)

2 Likes

I have found a solution to my own problem using remote event fires across multiple scripts. Thank you all for the help with this, I really appreciate it.

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