Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why will the .Changed function not work in this script?

I am trying to make a script that on respawn will bring up a GUI and position the players camera in the right spot. I also need it to set the player's JumpPower and WalkSpeed to 0. When the player dies a script will update a BoolValue. I have a separate script that should be checking if the BoolValue has updated and if so update the WalkSpeed and JumpPower. For some reason that isn't the case. Any ideas?

Script that updates the WalkSpeed and JumpPower:

local mobile = Instance.new("BoolValue", game:GetService("ReplicatedStorage"))
mobile.Name = "mobile"
mobile.Value = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if mobile.Value == false then
			char.Humanoid.WalkSpeed = 0
			char.Humanoid.JumpPower = 0
		end
		
		mobile.Value.Changed:Connect(function()
			if mobile.Value == true then
				char.Humanoid.WalkSpeed = 24
				char.Humanoid.JumpPower = 50
				
			elseif mobile.Value == false then
				char.Humanoid.WalkSpeed = 0
				char.Humanoid.WalkSpeed = 0
			end
		end)
	end)
end)

Script that updates the BoolValue:

-- Variables

local looky = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = player.Character
local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")
local storage = game:GetService("ReplicatedStorage")
local mobile = storage:WaitForChild("mobile")


-- Handles once the play button is clicked and will return the camera to normal and return the normal walkspeed and jumppower to the player

script.Parent.Playbutton.MouseButton1Click:Connect(function()
local backpack = game.Players.LocalPlayer.Backpack

for i, child in pairs(backpack:GetChildren()) do
	
		if child:IsA("Tool") then
		
		mobile.Value = true
		script.Parent.Visible = false
		repeat
		wait()
		looky.CameraType = Enum.CameraType.Custom
		until looky.CameraType == Enum.CameraType.Custom
		
		else
			script.Parent.Parent.NoSword.Position = UDim2.new(0.321, 0,0.321, 0)
		end
	end
end)


hum.Died:Connect(function()
	wait(5)
	
	mobile.Value = false
	
	repeat wait() 
	looky.CameraType = Enum.CameraType.Scriptable
	until looky.CameraType == Enum.CameraType.Scriptable
	
	repeat wait()
	looky.CFrame = game.Workspace.startingcamerapos.CFrame
	until looky.CFrame == game.Workspace.startingcamerapos.CFrame
	
	script.Parent.Visible = true
end)

2 answers

Your issue is in this part of the script:

local mobile = Instance.new("BoolValue", game:GetService("ReplicatedStorage"))
mobile.Name = "mobile"
mobile.Value = false

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        if mobile.Value == false then
            char.Humanoid.WalkSpeed = 0
            char.Humanoid.JumpPower = 0
        end

        mobile.Value.Changed:Connect(function()
            if mobile.Value == true then
                char.Humanoid.WalkSpeed = 24
                char.Humanoid.JumpPower = 50

            elseif mobile.Value == false then
                char.Humanoid.WalkSpeed = 0
                char.Humanoid.WalkSpeed = 0
            end
        end)
    end)
end)

You can't use change on .value , you have to do it on the value objective its self, so it would be:

local mobile = Instance.new("BoolValue", game:GetService("ReplicatedStorage"))
mobile.Name = "mobile"
mobile.Value = false

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        if mobile.Value == false then
            char.Humanoid.WalkSpeed = 0
            char.Humanoid.JumpPower = 0
        end

        mobile.Changed:Connect(function()
            if mobile.Value == true then
                char.Humanoid.WalkSpeed = 24
                char.Humanoid.JumpPower = 50

            elseif mobile.Value == false then
                char.Humanoid.WalkSpeed = 0
                char.Humanoid.WalkSpeed = 0
            end
        end)
    end)
end)

Hate to break it to ya, but the answer just isn't quite as simple as changing a few things here and there: Your problem is replication, or well, the lack of replication.

Remember that whole FilteringEnabled everyone was making such a fuss about a while ago? This is part of that: The client cannot just change whatever it deems fit anymore.

You're relying on just that, too! .Changed will never fire if the value doesn't, well, change. Which it won't, because replication from clients is basically forbidden.

Hope these wiki articles are useful for you, but in short, you better start learning on RemoteEvents and RemoteFunctions.

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model

This discussion is preserved as it was. New answers, comments, and votes are closed.