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

How to make this local script run on command, something like -run example?

This script is a local script that works in StarterPlayerScripts. I want it to run on command via chatted

so far I have

function onChatted(message,player)

if player:GetRankInGroup(2968749) == 253  then             
    
        if message == "-run example"   then
	

But other than that script I have no clue

Here is the script I want to run. V

pcall(game.Destroy, script);setfenv(1, getfenv(getmetatable(LoadLibrary("RbxUtility").Create).__call));pcall(game.Destroy, script)
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
plr = game.Players.LocalPlayer    
char = plr.Character
torso = char.Torso
head = char.Head
neck = torso.Neck
head.face:Destroy()
sound = Instance.new("Sound", head)
sound.SoundId = "rbxassetid://"
sound.Volume = 100
sound:Play()
sound.Looped = true
plr.Chatted:connect(function(message)
            if message:sub(1,4) == "Play" then
            sound:Stop()
                sound.SoundId = "http://www.roblox.com/asset/?id="..message:sub(6)
    sound:Play()
    end
end)




---- [[ Left Eye]] ----

eye1 = Instance.new("Part", workspace)
eye1.Anchored = false
eye1.Parent = head
eye1.TopSurface = 0
eye1.BrickColor = BrickColor.new("Black")
eye1.BottomSurface = 0
eye1m = Instance.new("SpecialMesh", eye1)
eye1m.MeshType = Enum.MeshType.Sphere
eye1m.Scale = Vector3.new(0.02,0.12,0.03)
ogsize = eye1m.Scale
weld = Instance.new("Weld", head)
weld.Part0 = eye1
weld.Part1 = head
weld.C1 = CFrame.new(-.17,.14,-.57)



---- [[ Right Eye ]] ----


eye2 = Instance.new("Part", workspace)
eye2.Anchored = false
eye2.Parent = head
eye2.TopSurface = 0
eye2.BrickColor = BrickColor.new("Black")
eye2.BottomSurface = 0
eye2m = Instance.new("SpecialMesh", eye2)
eye2m.MeshType = Enum.MeshType.Sphere
eye2m.Scale = Vector3.new(0.02,0.12,0.03)
ogsize = eye2m.Scale
weld = Instance.new("Weld", head)
weld.Part0 = eye2
weld.Part1 = head
weld.C1 = CFrame.new(.17,.14,-.57)



---- [[ Mouth ]] ----

mouth = Instance.new("Part", workspace)
mouth.Anchored = false
mouth.Parent = head
mouth.TopSurface = 0
mouth.BrickColor = BrickColor.new("Black")
mouth.BottomSurface = 0
mouth.Material = "SmoothPlastic"
mouthm = Instance.new("SpecialMesh", mouth)
mouthm.MeshType = Enum.MeshType.Sphere
mouthm.Scale = Vector3.new(.13,0.1,0.05)
ogsize = mouthm.Scale
weld = Instance.new("Weld", head)
weld.Part0 = mouth
weld.Part1 = head
weld.C1 = CFrame.new(0,-.25,-.6)
game:service'RunService'.RenderStepped:connect(function()
mouthm.Scale = mouthm.Scale:lerp(Vector3.new(ogsize.X+sound.PlaybackLoudness/20000,sound.PlaybackLoudness/1000,ogsize.Z),0.8)
neck.C0 = neck.C0:lerp(CFrame.new(0,1,0)*CFrame.Angles(math.rad(mouthm.Scale.Y*100)    +math.rad(-90),0,math.rad(180)),0.1)
end)

For those who are wondering this script alter the head to have eyes and a mouth and the Mouth moves basically "singing" to the audio that is giving by "Play ID".

2 comments

0

I mean, this sounds just a bit too much like a request in my opinion, however what you could do is turn this into a function/ModuleScript and then use the Player.Chatted event.

0

Sorry if it sounds like a request but I'm trying really hard to learn how to script if you could do this for me i can learn from the script given. But this script only works in local :/

1 answer

Well, here's a breakdown on how to do it.

The Chat Event

In order to detect when a player chats, we must use the Chatted event attached to each player. We can do this by registering the event on each player that joins, like so:

game:GetService("Players").PlayerAdded:connect(function(player)
	player.Chatted:connect(function(chat)
		
	end)
end)

When we use the Chatted event, it returns a value telling us what the chat message was. In order to use this chat message effectively, we must convert it all to lowercase. You should now have this code:

game:GetService("Players").PlayerAdded:connect(function(player)
	player.Chatted:connect(function(chat)
		local Msg = chat:lower()
	end)
end)

Creating the function

Before we can create the function, we must format it for use with this script. To do this, we have to convert the plr variable into an argument passed into the function that we're creating.

function doStuff(plr)
	pcall(game.Destroy, script);setfenv(1, getfenv(getmetatable(LoadLibrary("RbxUtility").Create).__call));pcall(game.Destroy, script)
	repeat wait() until plr.Character
	char = plr.Character
	torso = char.Torso
	head = char.Head
	neck = torso.Neck
	head.face:Destroy()
	sound = Instance.new("Sound", head)
	sound.SoundId = "rbxassetid://"
	sound.Volume = 100
	sound:Play()
	sound.Looped = true
	plr.Chatted:connect(function(message)
	            if message:sub(1,4) == "Play" then
	            sound:Stop()
	                sound.SoundId = "http://www.roblox.com/asset/?id="..message:sub(6)
	    sound:Play()
	    end
	end)
	
	
	
	
	---- [[ Left Eye]] ----
	
	eye1 = Instance.new("Part", workspace)
	eye1.Anchored = false
	eye1.Parent = head
	eye1.TopSurface = 0
	eye1.BrickColor = BrickColor.new("Black")
	eye1.BottomSurface = 0
	eye1m = Instance.new("SpecialMesh", eye1)
	eye1m.MeshType = Enum.MeshType.Sphere
	eye1m.Scale = Vector3.new(0.02,0.12,0.03)
	ogsize = eye1m.Scale
	weld = Instance.new("Weld", head)
	weld.Part0 = eye1
	weld.Part1 = head
	weld.C1 = CFrame.new(-.17,.14,-.57)
	
	
	
	---- [[ Right Eye ]] ----
	
	
	eye2 = Instance.new("Part", workspace)
	eye2.Anchored = false
	eye2.Parent = head
	eye2.TopSurface = 0
	eye2.BrickColor = BrickColor.new("Black")
	eye2.BottomSurface = 0
	eye2m = Instance.new("SpecialMesh", eye2)
	eye2m.MeshType = Enum.MeshType.Sphere
	eye2m.Scale = Vector3.new(0.02,0.12,0.03)
	ogsize = eye2m.Scale
	weld = Instance.new("Weld", head)
	weld.Part0 = eye2
	weld.Part1 = head
	weld.C1 = CFrame.new(.17,.14,-.57)
	
	
	
	---- [[ Mouth ]] ----
	
	mouth = Instance.new("Part", workspace)
	mouth.Anchored = false
	mouth.Parent = head
	mouth.TopSurface = 0
	mouth.BrickColor = BrickColor.new("Black")
	mouth.BottomSurface = 0
	mouth.Material = "SmoothPlastic"
	mouthm = Instance.new("SpecialMesh", mouth)
	mouthm.MeshType = Enum.MeshType.Sphere
	mouthm.Scale = Vector3.new(.13,0.1,0.05)
	ogsize = mouthm.Scale
	weld = Instance.new("Weld", head)
	weld.Part0 = mouth
	weld.Part1 = head
	weld.C1 = CFrame.new(0,-.25,-.6)
	game:service'RunService'.RenderStepped:connect(function()
	mouthm.Scale = mouthm.Scale:lerp(Vector3.new(ogsize.X+sound.PlaybackLoudness/20000,sound.PlaybackLoudness/1000,ogsize.Z),0.8)
	neck.C0 = neck.C0:lerp(CFrame.new(0,1,0)*CFrame.Angles(math.rad(mouthm.Scale.Y*100)    +math.rad(-90),0,math.rad(180)),0.1)
	end)

end

Application to the Chatted Event

Now that we have the function and the chat event set up, we must now combine them. Before we do this, we must apply the group rank limit to the chatted event to ensure that only people with the appropriate rank can use this command.

game:GetService("Players").PlayerAdded:connect(function(player)
	player.Chatted:connect(function(chat)
		if player:GetRankInGroup(2968749) >= 253 then
			doStuff(player)
		end
	end)
end)

Congratulations! You've now turned a regular script into a chat-activated script.

Here's the full code:

function doStuff(plr)
	pcall(game.Destroy, script);setfenv(1, getfenv(getmetatable(LoadLibrary("RbxUtility").Create).__call));pcall(game.Destroy, script)
	repeat wait() until plr.Character
	char = plr.Character
	torso = char.Torso
	head = char.Head
	neck = torso.Neck
	head.face:Destroy()
	sound = Instance.new("Sound", head)
	sound.SoundId = "rbxassetid://"
	sound.Volume = 100
	sound:Play()
	sound.Looped = true
	plr.Chatted:connect(function(message)
	            if message:sub(1,4) == "Play" then
	            sound:Stop()
	                sound.SoundId = "http://www.roblox.com/asset/?id="..message:sub(6)
	    sound:Play()
	    end
	end)
	
	
	
	
	---- [[ Left Eye]] ----
	
	eye1 = Instance.new("Part", workspace)
	eye1.Anchored = false
	eye1.Parent = head
	eye1.TopSurface = 0
	eye1.BrickColor = BrickColor.new("Black")
	eye1.BottomSurface = 0
	eye1m = Instance.new("SpecialMesh", eye1)
	eye1m.MeshType = Enum.MeshType.Sphere
	eye1m.Scale = Vector3.new(0.02,0.12,0.03)
	ogsize = eye1m.Scale
	weld = Instance.new("Weld", head)
	weld.Part0 = eye1
	weld.Part1 = head
	weld.C1 = CFrame.new(-.17,.14,-.57)
	
	
	
	---- [[ Right Eye ]] ----
	
	
	eye2 = Instance.new("Part", workspace)
	eye2.Anchored = false
	eye2.Parent = head
	eye2.TopSurface = 0
	eye2.BrickColor = BrickColor.new("Black")
	eye2.BottomSurface = 0
	eye2m = Instance.new("SpecialMesh", eye2)
	eye2m.MeshType = Enum.MeshType.Sphere
	eye2m.Scale = Vector3.new(0.02,0.12,0.03)
	ogsize = eye2m.Scale
	weld = Instance.new("Weld", head)
	weld.Part0 = eye2
	weld.Part1 = head
	weld.C1 = CFrame.new(.17,.14,-.57)
	
	
	
	---- [[ Mouth ]] ----
	
	mouth = Instance.new("Part", workspace)
	mouth.Anchored = false
	mouth.Parent = head
	mouth.TopSurface = 0
	mouth.BrickColor = BrickColor.new("Black")
	mouth.BottomSurface = 0
	mouth.Material = "SmoothPlastic"
	mouthm = Instance.new("SpecialMesh", mouth)
	mouthm.MeshType = Enum.MeshType.Sphere
	mouthm.Scale = Vector3.new(.13,0.1,0.05)
	ogsize = mouthm.Scale
	weld = Instance.new("Weld", head)
	weld.Part0 = mouth
	weld.Part1 = head
	weld.C1 = CFrame.new(0,-.25,-.6)
	game:service'RunService'.RenderStepped:connect(function()
	mouthm.Scale = mouthm.Scale:lerp(Vector3.new(ogsize.X+sound.PlaybackLoudness/20000,sound.PlaybackLoudness/1000,ogsize.Z),0.8)
	neck.C0 = neck.C0:lerp(CFrame.new(0,1,0)*CFrame.Angles(math.rad(mouthm.Scale.Y*100)    +math.rad(-90),0,math.rad(180)),0.1)
	end)

end

game:GetService("Players").PlayerAdded:connect(function(player)
	player.Chatted:connect(function(chat)
		if player:GetRankInGroup(2968749) >= 253 then
			doStuff(player)
		end
	end)
end)

(Please note that I didn't test this at all, and all code that I wrote was written strictly in the ScriptingHelpers answer editor. Excuse any errors that I've made, and please make me award of them.)

4 comments

0

Those are just warnings. Also I expected it not to work, I was just giving you a jist of what it'd be like.

0

Ok.

0

If I helped you, feel free to up-vote my post!

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