ROBLOX Get name of a player that clicked a brick - lua

I have this script in a brick:
local giver = 1
function onClicked()
game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value = game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Now I need to somehow get the player's name that clicked it and put it where I need to.

The ClickDetectors's MouseClick event have the "Clicking Player" as parameter, so you can do it like this:
local giver = 1
function onClicked(Player)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
However, this requires the FilteringEnabled to be set to false (not recomended).
To solve this, make a LocalScript in the brick with the code:
script.Parent.ClickDetector.MouseClick:connect(function(Player)
game.ReplicatedStorage:WaitForChild("BrickClick"):InvokeServer(script.Parent)
end)
And in a Script placed in the ServerScriptService put:
local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick")
if Listener == nil then
Listener = Instance.new("RemoteFunction")
Listener.Name = "BrickClick"
Listener.Parent = game.ReplicatedStorage
end
function Listener.OnServerInvoke(Player,Brick)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + 1
end
I won't point you to the wiki page for further reading, even thought it contains a bit of what you need, it contains too little information.
The ClickDetector's MouseClick info, the guide about FilteringEnabled and the guide about RemoteFunctions are better.

Try this!
script.Parent.MouseClick:Connect(function(Player)
-- Kill The Player
-- The parameter is referring to game.Players So if you want to do a kill button use .Character
Player.Character:BreakJoints()
-- Change The Color To Red (Other details)
script.Parent.Parent.BrickColor = BrickColor.new("Really red")
script.Parent.MaxActivationDistance = 0
-- Wait 4 Secs
wait(5)
-- Change The Color To Green
script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
script.Parent.MaxActivationDistance = 50
end)

Related

When ever I run this music script it works except for changing the GUI text to the current song name

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
local track = game.StarterGui.MusicPlayer.Track
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local d = b[c]
while true do
wait()
d:Play()
local connection = mutebutton.MouseButton1Click:Connect(function()
if d.IsPaused then
d:Resume()
mutebutton.Text = "Pause"
else
d:Pause()
mutebutton.Text = "Resume"
end
end)
d.Ended:Wait()
connection:Disconnect()
d:GetPropertyChangedSignal("SoundId"):Connect(function()
track.Text = d.Name
end)
end
Everything works until the Get Property Changed Signal event. Whenever I load into the game the box that should have the song name will instead not do anything and keep it as an empty box. Anyone know why?
It seems you have a single mute button (not one for each song), so why are you putting the pausing functionality inside a while loop?
You're not changing the sound id anywhere, is there more code that you're not showing?
You have a variable "local d" but this is only ever calculated outside of the loop so it will just repeat the same song over.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
local track = game.StarterGui.MusicPlayer.Track
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local song -- definite it originally here as nil so we can access it outside of the loop
mutebutton.MouseButton1Click:Connect(function()
if song.IsPaused then
song:Resume()
mutebutton.Text = "Pause"
else
song:Pause()
mutebutton.Text = "Resume"
end
end)
while true do
song = b[math.random(1, #b)]
track.Text = song.Name
song:Play()
song.Ended:Wait()
task.wait()
end
This might work, but I can't see your Explorer

Roblox script running multiple times?

I need a bit of help. Basically I have this code:
local plyIsEntered = false
function onTouched(hit)
plyIsEntered = true
if not plyIsEntered then
end
if plyIsEntered then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local ply = humanoid.Parent
if humanoid ~= nil then
print("Hit")
local playerName = hit.Parent.Name
print(playerName)
local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter")
local scanLaser = Instance.new("Part", game.Workspace)
scanLaser.Position = laserEmitter.Position
scanLaser.Name = "ScanLaser"
scanLaser.Size = Vector3.new(1,1,1)
local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser)
scanLaserMesh.Name = "Cone mesh"
scanLaserMesh.MeshType = ""
plyIsEntered = false
end
end
end
script.Parent.Touched:connect(onTouched)
Now I'm checking if the player touches a box, it has no collisions and is invisible; when they do I want to create a laser that will scan them and open a door. The problem I'm having is when I walk into the trigger box it creates 8 or 9 blocks. One of those blocks is the block I'm applying a mesh too.
What I need to do is make sure it's only running once and not creating more than 1 brick. Hopefully someone can help me!
I believe to fix this you'll need to add a debounce.
You see, the touched event of a Part actually fires many times, so your code will execute multiple times if it is inside of the event.
To fix this, we use a debounce, which means your code won't execute if your part is touched too much in the same time frame. Here is an example:
local debounce = false
part.Touched:connect(function()
if debounce == false then
debounce = true
--Your code goes here.
wait(1)--Wait one second until you'll be able to execute the code again.
debounce = false
end
end)
To read more on debounces: http://wiki.roblox.com/index.php?title=Debounce

A lua script on roblox that moves a model upwards?

I have a model called door
Inside I have a BoolValue named Open
I have a model called Top that has all of the door blocks named Work Mabey Comeon and Proboblynot
And I have Block that when touched is supposed to make Top move up
Directly inside door I have this script
door = script.Parent
open = door.Open
Top = door.Top
opener = 18
speed = 100
steps = speed
startl = Top.CFrame
function MoveDoorToCFrame(cfrm,dr)
dr.Work.CFrame = cfrm
dr.Mabey.CFrame = dr.Work.CFrame * CFrame.new(0,-7.2,0)
dr.Comeon.CFrame = dr.Work.CFrame * CFrame.new(0,10.8,0)
dr.Problynot.CFrame = dr.Work.CFrame * CFrame.new(0,10.8,0)
end
function Update()
if speed/steps < 0.5 then
calc = 1-math.cos(math.rad((-90/speed)*steps*2))
else
calc = 1+math.sin(math.rad((90/speed)*((speed/2)-steps)*2))
end
MoveDoorToCFrame(startl * CFrame.new(0,(calc/2)*opener,0),Top)
end
Update()
while true do
wait()
if not open.Value and steps < speed then
steps = steps + 1
Update()
elseif open.Value and steps > 0 then
steps = steps - 1
Update()
end
end
Inside the button that is supposed to activate on touch I have
script.Parent.Touched:connect(function()
script.Parent.Parent.Open.Value = not script.Parent.Parent.Open.Value
end)
script.Parent.Parent.Open.Changed:connect(Update)
Update()
If you know how to fix this it would be gladly appreciated.
Update November 2015:
Using PrimaryPart
Since writing this post, ROBLOX has changed a lot in regards to the API. To move a model like requested, you should set the PrimaryPart property of the model to a central part inside the model. This will act as the origin for the model's movements.
You can then use model:SetPrimaryPartCFrame(cframe) to set the CFrame of the model. You can also retrieve this property by using model:GetPrimaryPartCFrame(), although I believe that is just a shortcut method for model.PrimaryPart.CFrame.
In code, it would look like this:
-- Set PrimaryPart:
MODEL.PrimaryPart = MODEL.SomeCentralPart
...
-- CFrame movement:
local movement = CFrame.new(0, 10, 0)
-- Move the model:
MODEL:SetPrimaryPartCFrame(MODEL:GetPrimaryPartCFrame() * movement)
Option A: Use Model's methods
I think you are making this much more difficult than it needs to be. Whenever you run into an issue like this, be sure to check the current APIs provided. The ROBLOX Model object contains a nifty method called 'TranslateBy' which takes a Vector3 argument to translate the model.
Using MODEL:TranslateBy(Vector3) is similar to moving a model via CFrame, since it ignores collisions.
Another alternative is MODEL:MoveTo(Vector3) which moves a whole model to the given Vector3 world position. The downside to this is that it does collide.
One way to get the same MoveTo effect but without collisions can be done with the TranslateBy method:
MODEL:TranslateBy(Vector3Position - MODEL:GetModelCFrame().p)
Option B: Write a custom function to manipulate the model's CFrame
Another alternative would be to manipulate the whole model's CFrame entirely. To do this, you can write a clever function that will move a whole model relative to an 'origin' point. This is similar to moving shapes on a grid given their points and an origin, except in three dimensions. Using ROBLOX's built-in functions, this is much easier though.
A good way to do this would be to write a function that lets you actually assign a CFrame value to a whole model. Another way would be to allow a translation via CFrame too.
Here's an example:
function ModelCFrameAPI(model)
local parts = {} -- Hold all BasePart objects
local cf = {} -- API for CFrame manipulation
do
-- Recurse to get all parts:
local function Scan(parent)
for k,v in pairs(parent:GetChildren()) do
if (v:IsA("BasePart")) then
table.insert(parts, v)
end
Scan(v)
end
end
Scan(model)
end
-- Set the model's CFrame
-- NOTE: 'GetModelCFrame()' will return the model's CFrame
-- based on the given PrimaryPart. If no PrimaryPart is provided
-- (which by default is true), ROBLOX will try to determine
-- the center CFrame of the model and return that.
function cf:SetCFrame(cf)
local originInverse = model:GetModelCFrame():inverse()
for _,v in pairs(parts) do
v.CFrame = (cf * (originInverse * v.CFrame))
end
end
-- Translate the model's CFrame
function cf:TranslateCFrame(deltaCf)
local cf = (model:GetModelCFrame() * deltaCf)
self:SetCFrame(cf)
end
return cf
end
-- Usage:
local myModel = game.Workspace.SOME_MODEL
local myModelCF = ModelCFrameAPI(myModel)
-- Move to 10,10,10 and rotate Y-axis by 180 degrees:
myModelCF:SetCFrame(CFrame.new(10, 10, 10) * CFrame.Angles(0, math.pi, 0))
-- Translate by 30,0,-10 and rotate Y-axis by 90 degrees
myModelCF:TranslateCFrame(CFrame.new(30, 0, -10) * CFrame.Angles(0, math.pi/2, 0))
This might be hard.
You might want to look to free models for this one unless the people above get it to work.
I, however, do have a script to move a model:
game.Workspace.Model:MoveTo(Vector3.new(0,0,0))
Your code indeed needs fixing.
You should NOT use a never-ending loop to make your stuff work (unless that is the only way).
You should rather base actions on events.
Consider to use this:
Structure:
Door [Model]
DoorScript [Script]
Button [Part]
DoorOpen [BoolValue]
Top [Model]
Mabey [Part]
Comeon [Part]
Problynot [Part]
DoorScript:
local Model = script.Parent
local Door = Model.Top
local Button = Model.Button
local DoorOpen = Model.DoorOpen
local Offset = 0
local ToOffset = 100
local Direction = 1
local StepLength = 0.1
local Moving = false
function StartMoving()
if Moving then return end
Moving = true
while (DoorOpen.Value and Offset ~= ToOffset) or (not DoorOpen.Value and Offset ~= 0) do
local Change = Offset
Offset = math.max(0,math.min(ToOffset,Offset + StepLength * (DoorOpen.Value and 1 or -1)))
Change = Offset - Change
Top:TranslateBy(Vector3.new(0,Change,0))
wait()
end
Moving = false
end
StartMoving()
DoorOpen.Changed:connect(StartMoving)
local Debounce = false
Button.Touched:connect(function()
if Debounce then return end
Debounce = true
DoorOpen.Value = not DoorOpen.Value
wait(4)
Debounce = false
end)
You might want to adjust the speed tho.
This can be used to move models, try adding something like this into your code. It's more dynamic.
a = Workspace.Model
for i=0.1,40 do
for i,v in pairs(a:getChildren()) do
if v:IsA("Part") then
v.CFrame = CFrame.new(v.CFrame + Vector3.new(0,0.1,0))
else print("Not a part")
end
end
end

how to detect touch area in corona sdk?

In my application i want to count the touches.but when i touch that image that has to be cover some area then only count will be increse.plese help me
function pop(event)
if event.phase=="began" then
elseif event.phase == "ended" then
numValue=numValue+1
print("tapcount value"..numValue)
count.text=numValue
end
end
lips:addEventListener("touch",pop)
Do you want to count the taps or the touches? If it's taps, which is what it looks like, take a look at this tutorial for doing just that: http://corona.techority.com/2012/07/30/how-to-use-double-tap-in-your-corona-app/ I know the title says "double tap" but it will work for any kind of tap counting.
Is this is what you are looking for..?
local obj_1 = ..... ; obj.tag = 1
local lips = ..... ; lips.tag = 2
local count = .....
local numValue = 0
local clickedObj_tag = 0
local function pop(e)
if(clickedObj_tag~=e.target.tag)then
clickedObj_tag = e.target.tag
-- cover/highlight your object
else
numValue=numValue+1
print("tapcount value"..numValue)
count.text=numValue
-- do rest of your code
end
lips:addEventListener("touch",pop)
obj_1:addEventListener("touch",pop)

Getting a button reference?

I'm working on an app in Lua + Corona. As a complete beginner, I've managed to hack together a little script for a carousel, but now I've got a question.
function forwardButtonPress()
if carousel.getCurImage() < #myImages then
carousel.slideToImage(carousel.getCurImage() + 1)
end
end
function backButtonPress()
if carousel.getCurImage() > 1 then
carousel.slideToImage(carousel.getCurImage() - 1)
end
end
--Here's where we do the actual initilization of the page.
local fwbutton = display.newImage("buttonArrow.png")
fwbutton.x = 260
fwbutton.y = 120
fwbutton:addEventListener("tap", forwardButtonPress )
local bkbutton = display.newImage("buttonBackArrow.png")
bkbutton.x = 60
bkbutton.y = 120
bkbutton:addEventListener("tap", backButtonPress )
If you look at the code, you'll see that I have two buttons, a back button and a forward one. Those are for sliding the images. So, say you get to the end of the carousel. The script takes care of making sure that it doesn't go past the end already, but how do I access the button to set the alpha to zero or fade it? It's linear, so I can't just put the button above its event function, so that the event function can reference the button... is there a way to pass the event function a reference to the button?
You can forward declare the event handler functions like this at the top of the file:
local forwardButtonPress
local backButtonPress
Then create your buttons and attach the event handlers (This is your code copied & pasted):
local fwbutton = display.newImage("buttonArrow.png")
fwbutton.x = 260
fwbutton.y = 120
fwbutton:addEventListener("tap", forwardButtonPress )
local bkbutton = display.newImage("buttonBackArrow.png")
bkbutton.x = 60
bkbutton.y = 120
bkbutton:addEventListener("tap", backButtonPress )
Add a function to manage setting the appearance of the buttons when either button is clicked:
local function setButtons()
if carosel.getCurImage() < #myImages then
fwbutton.alpha = 1.0
else
fwbutton.alpha = 0.5
end
if carosel.getCurImage() > 1 then
bkbutton.alpha = 1.0
else
bkbutton.alpha = 0.5
end
end
Now, you can write the function implementations, which will be able to deal with the buttons via the setButtons function:
forwardButtonPressed = function()
if carousel.getCurImage() < #myImages then
carousel.slideToImage(carousel.getCurImage() + 1)
end
setButtons()
end
backButtonPress = function()
if carousel.getCurImage() > 1 then
carousel.slideToImage(carousel.getCurImage() - 1)
end
setButtons()
end
Disclaimer: I'm not able to test this now, so there might be a syntax error somewhere, but organizing the code this way will work for what you're doing.
You can create/define the buttons above the function, and attach the EventListener below, no? If not, I don't really understand the problem.

Resources