Gui player move

Hey, i want to make a mini game that will use gui for this,it will change player script when u click a or d, to make a walk animaiton,
but problem is that script says "aw is not a valid member of ScreenGui “StarterGui.ScreenGui”, while aw is a part of screengui, here’s a script

local aw = game.StarterGui.ScreenGui.aw
local UserInputService = game:GetService("UserInputService")
local ScreenGui = game.StarterGui.ScreenGui
UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		if     key == Enum.KeyCode.W then game.StarterGui.ScreenGui.aw = (if UserInputService:IsKeyDown(Enum.KeyCode.W) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011")
		elseif key == Enum.KeyCode.A then game.StarterGui.ScreenGui.aw = (if UserInputService:IsKeyDown(Enum.KeyCode.A) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738")
		elseif key == Enum.KeyCode.S then game.StarterGui.ScreenGui.aw = (if UserInputService:IsKeyDown(Enum.KeyCode.S) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011")
		elseif key == Enum.KeyCode.D then game.StarterGui.ScreenGui.aw = (if UserInputService:IsKeyDown(Enum.KeyCode.D) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738")
		end
	end
end)

Screenshot 2022-04-30 at 21.34.01
Why it says like that?
Also, when i use only one equals there
Screenshot 2022-04-30 at 21.36.24
it emphasizes, also elseif, and key
Screenshot 2022-04-30 at 21.37.17

1 Like

The problem with this

is that the screengui may not yet be registering “aw” as a descendant.
A solution to this is putting a :WaitForChild() in between it:

game.StarterGui.ScreenGui:WaitForChild("aw")

P.S. Correct me if I’m wrong but I believe when making changes to GUIs that you should use localscripts referring to the playergui!

1 Like

So it should look like this?


UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		if     key == Enum.KeyCode.W then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.W) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011")
		elseif key == Enum.KeyCode.A then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.A) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738")
		elseif key == Enum.KeyCode.S then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.S) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011")
		elseif key == Enum.KeyCode.D then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.D) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738")
		end
	end
end)

Note if you are trying to make a change that true player will see, you must use player.PlayerGui rather than StarterGui

First off, if you want to change the GUI for a player, you would change the Player.PlayerGui, not the StarterGui. Next, your “if” statements are all wrong. You can not have multiple "then"s in a single statement. Conditional Statements. Lastly, please make your “if” statements into multi-line code. It is very hard to read and will become confusing very quickly as it is.

1 Like

Sure, but I think using a variable would help a lot. And in here where you write:

key == Enum.KeyCode.W then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.W) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011"

I am not sure what you’re trying to set “aw” to in the middle of the code…

This code is so messy. You need to learn how to make it look clean and indented.

I’m very bad at making scripts

It’s okay! We all start somewhere, but I do think the source @noblemaxx provided would really help!

player.localplayer.playergui? )1123123123(

Here I cleaned it up a bit.

UserInputService.InputBegan:Connect(function(input, processed)
if processed then return end
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local key = input.KeyCode
			if key == Enum.KeyCode.W then game.StarterGui.ScreenGui:WaitForChild("aw") == (if UserInputService:IsKeyDown(Enum.KeyCode.W) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011")
				elseif key == Enum.KeyCode.A then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.A) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738")
				elseif key == Enum.KeyCode.S then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.S) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011")
				elseif key == Enum.KeyCode.D then game.StarterGui.ScreenGui:WaitForChild("aw") = (if UserInputService:IsKeyDown(Enum.KeyCode.D) then game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image == "rbxassetid://9508139738")	
		end
	end
end)

I fixed the code, It works try it!

local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(input, processed)
	if processed then return end
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local key = input.KeyCode
		if key == Enum.KeyCode.W then
			game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.W) then game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" end
				elseif key == Enum.KeyCode.A then game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.A) then game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" end
				elseif key == Enum.KeyCode.S then game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.S) then game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" end
				elseif key == Enum.KeyCode.D then game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.D) then  game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" end	
		end
	end
end)

I press a/d buttons, texture not changing, i will try changing screengui to player gui

I got rid of all the errors but that doesn’t necessarily make the code work. Maybe it was a mistake on your end.

Here full script:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local aw = game.StarterGui.ScreenGui.aw
local ScreenGui = game.Players.LocalPlayer.PlayerGui.ScreenGui
local posX = 0
local posY = 0
local velocityX = 0
local velocityY = 0

local mover = script.Parent
local speed = 100 


RunService.RenderStepped:Connect(function(dt)

	posX = posX + velocityX * dt * speed
	posY = posY + velocityY * dt * speed
	mover.Position = UDim2.new(
		0, posX,
		0, posY
	)
end)
local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		if key == Enum.KeyCode.W then
			game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.W) then game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" end
		elseif key == Enum.KeyCode.A then game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.A) then game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" end
		elseif key == Enum.KeyCode.S then game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.S) then game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" end
		elseif key == Enum.KeyCode.D then game.StarterGui.ScreenGui:WaitForChild("aw") if UIS:IsKeyDown(Enum.KeyCode.D) then  game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508563011" else game.StarterGui.ScreenGui.aw.Image = "rbxassetid://9508139738" end	
		end
	end
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		
		if     key == Enum.KeyCode.W then velocityY = (if UserInputService:IsKeyDown(Enum.KeyCode.S) then 0 else -1)
		elseif key == Enum.KeyCode.A then velocityX = (if UserInputService:IsKeyDown(Enum.KeyCode.D) then 0 else -1)
		elseif key == Enum.KeyCode.S then velocityY = (if UserInputService:IsKeyDown(Enum.KeyCode.W) then 0 else  1)
		elseif key == Enum.KeyCode.D then velocityX = (if UserInputService:IsKeyDown(Enum.KeyCode.A) then 0 else  1)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode

		if     key == Enum.KeyCode.W then velocityY = (if UserInputService:IsKeyDown(Enum.KeyCode.S) then  1 else 0)
		elseif key == Enum.KeyCode.A then velocityX = (if UserInputService:IsKeyDown(Enum.KeyCode.D) then  1 else 0)
		elseif key == Enum.KeyCode.S then velocityY = (if UserInputService:IsKeyDown(Enum.KeyCode.W) then -1 else 0)
		elseif key == Enum.KeyCode.D then velocityX = (if UserInputService:IsKeyDown(Enum.KeyCode.A) then -1 else 0)
		end
	end
end)

also
thing.rbxm (4.4 КБ)

Your script don’t give me any errors, but doesn’t change texture :slightly_frowning_face:

I think that the wrong line might be here.

if input.UserInputType == Enum.UserInputType.Keyboard then

Instead of using that I use

if input.KeyCode == Enum.KeyCode.W then -- your key here

It should look like this?

Screenshot 2022-04-30 at 22.04.01

Or this

Screenshot 2022-04-30 at 22.05.06

Oh, I messed up. I meant to say:

if Input.KeyCode == Enum.KeyCode.W then

It’s emphasized :slightly_frowning_face: (11112312312)

Did you put an end for that line?