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

Why doesnt this update the speed? SOLVED

local script


local character = game.Players.LocalPlayer.Character
local originalspeed = 16
game.Players.LocalPlayer.Chatted:Connect(function(msg)
if msg:lower():match("tp") then
if game.Players:FindFirstChild(msg:sub(4)) then
game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(game.Players:FindFirstChild(msg:sub(4)).Character.Head.Position)
end
end

if msg:lower():match("walkspeed") then
local newspeed = msg:sub(10)
print(newspeed)
	game:GetService("UserInputService").InputBegan:connect(function(input,gameprocesed)
 if input.KeyCode == Enum.KeyCode.LeftShift then
  for i = 1,16 do
	print(game.Players.LocalPlayer.Character.Humanoid.WalkSpeed)
	if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") then
  game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = newspeed
end
end
end
	  end)
	end
end)

game:GetService("UserInputService").InputEnded:connect(function(input,gameprocesed)
 if input.KeyCode == Enum.KeyCode.LeftShift then
  for i = 1,16 do
	if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") then
   game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
game.Players.LocalPlayer.Character.Humanoid.JumpPower = 50
end
end

output: 16

500 (x16)

50 (x15)

5 comments

0

You can only access 1 (not 16) character from local script. Even if you access other character (via game.Players:GetPlayers()), filtering enabled will prevent any changes to them.

0

nope, local scripts can change walkspeed and jumpspeed etc. done it several times

0

well, even if it changes the speed, it's only visible to the local player but not other player

0

The property change is, yes, but the speed effects still replicate. This is still very bad code, you have declared a character variable but never used it

0

ye idk why i made that variable

3 answers

that's pretty messy code; but it outputs thinks like 50(x16) because of this unnecessary chunk of code

  for i = 1,16 do
   	 print(game.Players.LocalPlayer.Character.Humanoid.WalkSpeed)
   	 if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") then
  		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = newspeed
	end
end

your code also accesses other players through LocalScript which will only cause side effects on your screen and not other players; for instance;

if the following code finds the player after the 4th index of the message, it will only teleport the player on your screen and not theirs; so i recommend doing anything that involves other players with Remote Events

plus: i recommend looping through the players and using player.Name:lower() to find the player after the 4th index, b/c it's unlikely that the message has the correct capitalization of the player's name which will cause FindFirstChild() to return nil

game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(game.Players:FindFirstChild(msg:sub(4)).Character.Head.Position)

so i recommend rewriting you code like this

local original_speed = 16
local player = game.Players.LocaPlayer
local teleportRemote_Event = game.ReplicatedStorage.etc.etc -- change to directory of a remote event in  replicated storage
local speedRemoteEvent = game.ReplicatedStorage.etc.etc -- change this too!
local input_service =  game:GetService("UserInputService")

player.Chatted:Connect(function(msg)
	local teleport = msg:match("tp")
	local walkspeed = msg:match("walkspeed")
	
	if teleport then
		local subjectPlayer = msg:sub(#teleport+1):lower()
		teleportRemote_Event:FireServer(subjectPlayer)
	
	elseif walkspeed then
		local speed = tonumber(msg:sub(#walkspeed+1):match("%d+"))
		speedRemoteEvent:FireServer(speed)
	end
	
	
end)

in a server script:

local teleportRemote_Event = game.ReplicatedStorage.etc.etc -- change to directory of a remote event in  replicated storage
teleportRemote_event.OnServerEvent:Connect(function(player,subject)
	local char = player.Character
	local subChar
	
	for _, plr in pairs(game.Players:GetChildren()) do
		if plr.Name:lower() == subject then
			subject = plr
			break
		end
	end

	subject or return 0;
	subChar = subject.Character

	if char and subChar then
		subChar.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame)

	end
end)

in another server script:

local speedRemoteEvent = game.ReplicatedStorage.etc.etc -- change this too!
speedRemoteEvent.OnServerEvent:connect(function(plr,speed)
	local char = player.Character
	local humanoid;

	if char:FindFirstChild("Humanoid") then
		humanoid = char.Humanoid
		humanoid.Walkspeed = speed
	end
end)

please note: i am at school, so there might be uncaught errors in my example code

4 comments

0

sorry but this dont help, i want it to function in a local script only and that is possible

0

and this is unnecessary aswell and you never need to use a server script to move the local player, that can be done in a local script

0

if you want it all in loca script, then you

0

if you want it all in loca script, then you would have to disable filtering enabled in order forall the players in the game to see the effects, otherwise, you would need to use server script. just sayin

I pasted your code and it says it is erroring. As much as we want to help you, we cannot because your formatting is unreadable.

Solution

A lot of your code could be shortened if you invest your time in making a system to create multiple commands easily. I highly recommend this tutorial made by EmeraldSlash. It'll show you how you should organize commands and more:

https://devforum.roblox.com/t/how-to-make-basic-admin-commands/59691

After reading this and understanding it, you can return arguments such as a Username and the new WalkSpeed and have them set from there.

If you want to connect things with a local script, use remote events.

Shortening your code

Now for your code, it is hard to read. You need to start making variables to stop calling the same family tree. It makes your code unpleasing to look at as well as making you waste time.

What you are doing:

game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
game.Players.LocalPlayer.Character.Humanoid.JumpPower = 50
game.Players.LocalPlayer.Character.Humanoid.MaxHealth = 100
game.Players.LocalPlayer.Character.Humanoid.Health= 100

game.Players.LocalPlayer.Chatted:Connect(function()

end)

if (game.Players.LocalPlayer.Character:FindFirstChild('Humanoid')) then

end

What you should be doing

local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character
local Humanoid = Character:WaitForChild('Humanoid')

Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
Humanoid.MaxHealth = 100
Humanoid.Health = 100

LocalPlayer.Chatted:Connect(function()

end)

if (Character:FindFirstChild('Humanoid')) then

end

See how much better that looks and how much faster you can type it?

Formatting

Fix your formatting. It will make your code easier to read for us as well as yourself. Your blocks should be aligned with each other.

Don't do

if (true) then
if (false) then
part.Touched:Connect(function()
part:Destroy()
end)
else
for i=1, 10 do
print(i)
end
end
end

That is really hard to read what is going on.

Do

if (true) then
	if (false) then
		part.Touched:Connect(function()
			part:Destroy()
		end)
	else
		for i=1, 10 do
			print(i)
		end
	end
end

We all can easily read each block and catch errors easily as well.

Loops

Looking at your code, it seems like you are using useless for loops. If you want it to slowly rise up, you need to add a wait.

--this
for i=1, 16 do
	Humanoid.WalkSpeed = 16
end

--and this
for i=1, 16 do
	Humanoid.WalkSpeed = i
end

--is the same as this:
Humanoid.WalkSpeed = 16

The loops run instantly because you don't make them wait. Don't use for loops if you aren't adding waits.

i reconfigured my script so it now works, here is how it should be

local player = game.Players.LocalPlayer
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
local originalspeed = 16
local newspeed = 16

player.Chatted:Connect(function(msg)
	if msg:lower():match("tp") then
		if game.Players:FindFirstChild(msg:sub(4)) then
			character.HumanoidRootPart.CFrame = CFrame.new(game.game.Players:FindFirstChild(msg:sub(4)).Character.Head.Position)
		end
	end
	if msg:lower():match("walkspeed") then
		newspeed = msg:sub(11)
		print(newspeed)
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.Q then
		print(newspeed)
		character.Humanoid.WalkSpeed = newspeed
	end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.Q then
		character.Humanoid.WalkSpeed = originalspeed
	end
end)
end)

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