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

Code Not Functioning?

The code doesnt work, or show any errors? please help me fix it! thanks for reading! im trying to clone a sword into the players invintory and have a value inside the local script with a texture to change the swords mesh texture! THX!

if game.workspace.Gamestart.Value == true then
	 local sword = game.ReplicatedStorage.ClassicSword:Clone()
	 sword.Parent = game:GetService("Players").LocalPlayer.Backpack
	 sword.Handle.Mesh.TextureId = script:WaitForChild("TextureId").Value
end

1 comment

0

ioutrageous, this is your last warning. If you continue to post duplicate Questions rather than continue feedback on your old ones, I will have no choice but to ban you from Scripting Helpers. If you don't know how to, send me or any of the admins a PM on ROBLOX.

2 answers

Your code is probably checking that once, finding it false, and never executing. There are two ways to check if a value has changed: use the .changed event or use a while loop to check every so often.

The first method is generally always better than the second, and is used like so:

function gameStart() --the stuff you want to do when Gamestart is true
	local sword = game.ReplicatedStorage.ClassicSword:Clone()
	sword.Parent = game:GetService("Players").LocalPlayer.Backpack
	sword.Handle.Mesh.TextureId = script:WaitForChild("TextureId").Value
end

if workspace.Gamestart.Value == true then gameStart() end --runs an initial check to see if it's already true

local connector
connector = workspace.Gamestart.Changed:connect(function(val) --runs every time the value is changed
	if val == true then gameStart() connector:disconnect() end --calls the function only if the value is true and essentially turns off the checking
end)

By the way, workspace is a defined variable that returns the same thing as game.workspace, so that can shorten a few lines.

#Problems

  • If then statement checking once
  • When it runs the value is false
  • code wont run

#Changed Instead of looping to check it or something we can see if the value changes:

workspace.Gamestart.Changed:connect(function(a)
	print("Gamestart's "..a.." has been changed to "..workspace.Gamestart[a])
end)


#Final Product

workspace.Gamestart.Changed:connect(function(val)
	if val == "Value" and game.workspace.Gamestart.Value then
		local sword = game.ReplicatedStorage.ClassicSword:Clone()
	     	sword.Parent = game:GetService("Players").LocalPlayer.Backpack
		sword.Handle.Mesh.TextureId = script:WaitForChild("TextureId").Value
	end
end



Hope it helps!

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