How to get the Roblox Proximity to open a store Frame - lua

I read through the Roblox Developer Hub on how to make a ProximityPrompt work, and I can get it to do certain things, but I want to be able to open in game gui stores with one. I can't find how to call into the Frame to make it visible, and I can't find how to get the Frame to look for the correct trigger.
Below is what I added to the Frame localScript
local prompt = game:GetService("ProximityPromptService")
local button = game.Workspace.MinerStore.ProximityPrompt.Triggered:WaitForChild()
local function prompt(PromptObject, player)
frame.Visible = not frame.Visible
And then this is what my Prompt script shows.
local frame = game.StarterGui.Miners:WaitForChild("Frame")
game.Workspace.MinerStore.ProximityPrompt.Triggered:Connect(function(player)
--game.StarterGui.Miners.Frame:Connect(function(player)
-- frame.Visible = not frame.Visible
--end)
end)
I took all of this right from the developer hub and tried to make it my own, but for reasons I don't understand the two don't connect.

Your commented out code shows that you are making a common mistake. UI that is placed in the StarterGui acts as a template. It is copied into each player's PlayerGui when their character spawns. You appear to be trying to modify the UI template, not the actual UI that the specific player sees.
Since a ProximityPrompt can be observed in a LocalScript, you can directly listen for the trigger in the UI LocalScript.
local prompt = game.Workspace.MinerStore.ProximityPrompt
local frame = script.Parent
prompt.Triggered:Connect(function()
frame.Visible = true
end)

Related

My chest script won't add the gold to my stats

game.Players.PlayerAdded:Connect(function(player)
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
local players = game:GetService("Players")
local clone = game.Workspace.Sparkles:Clone()
clone.Parent = game.Workspace
clone.CanCollide = false
clone.Position = script.Parent.Position
script.Parent:Destroy()
wait(1)
clone:Destroy()
local player = game.players.LocalPlayer
players.LocalPlayer.leaderstats.gold.value = 10
end
end)
end)
Here, I'm trying to make a script where if you touch a chest it will give you gold[my stat name] but for some reason, it wont run properly. it wont give me the amount of gold i told it to
From my understanding, you have a function that fires whenever a player joins the game. And then when the part is touched, the part then fires the code you have. I would remove the game.Players.PlayerAdded:Connect(function(player) function as when it get's hit it will fire for every single player who has joined.
But why am I not gaining any gold?
If you are using a server sided script you can't just call game.Players.LocalPlayer as that is client sided. LocalPlayer is an object that is in client sided scripts that tell the client sided script(s) what player they are executing on.
How do I fix this?
First, you already have a statement to decide whether the hit part is a character. So, you can do,
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
Player.leaderstats.gold.Value += 10
I've fixed a few things in your scripts and removed some things as well. First, I added a += 10 to the gold adding script as if you don't it will just set the gold value to 10 and never add any more.
LocalPlayer is not available to be used on server sided scripts and should only be used on Client sided.
(For future references, in your first initial code you have code that sets the variable of player but under that line you call players. (however, that wouldn't still work. Just trying to provide information about code that's syntax was incorrect.))
When you say script.Parent:Destroy() thats going to delete the script's parent right? So it's going to destroy the script's parent's children which includes the script, which means it can't give you the gold.
Tip: If you ever encounter an error, it might help to put some print statements for logging. For example in your code:
game.Players.PlayerAdded:Connect(function(player)
print("Player has joined!")
Just repeat this for every function, loop, if statement, etc.(with different print messages of course) and then test this and see what gets printed. Also put a print statement after you destroy the script's parent to check if the script stays or not.

How do i create a automatic find player script

so I am learning Lua at the moment and wanted to make something that if the player presses f it would print something. I know how to use input service. but what I DO NOT KNOW is how get the server to automatically detect the player. so a combat game for example.
you join with controls already there . I'm really confused!
local player = game:getservice("players")
local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
print("hi")
end
end)
You don't need to detect when a player joins to make your controls work. The problem is likely that your code is in a Script, and not a LocalScript. Scripts run on the server, and LocalScripts run on each individual client. Plus, user input needs to be detected on the client as well.
So, create a LocalScript in StarterPlayer > StarterCharacterScripts. This will replicate the code into each player when their Character spawns into the world. Then, move your existing code in there.
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
print("hi")
end
end)

How do I make a sell script? (Roblox)

I have been making a Roblox simulator as a side project to learn how to make games, Now I have gotten some help as you can see below but it is still not working. I have got some pictures and videos in these links:
https://flickr.com/photos/195497771#N05
https://vimeo.com/user173767075
Currently my issue is still the title of my question and my tool not working. I changed it to a remote event and now it stopped changing my leaderstats. I have tried a bunch of different solutions but none work. My first script is my tool script. Here it is:
local player = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
if player.Debounce.Value == false then
game.ReplicatedStorage.Power:FireServer(script.Parent.Values)
local action = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation)
action:Play()
end
end)
Now, that is in a LocalScript and that works. It prints that it activated and the animation plays. Now my problem is with a script in ServerScriptService not receiving the remote event. I have a print in it but nothing happens. Here is that script:
game.ReplicatedStorage.Power.OnServerEvent:Connct(function(player, valueFolder)
if player.Debounce.Value == false then
player.leaderstats.Sticks.Value += valueFolder.Power.Value
player.Debounce.Value = true
wait(valueFolder.Cooldown.Value)
player.Debounce.Value = false
end
end)
So, for my original question, the sell knows I touch it and it activates the event but nothing changes in the leaderstats. Here is my script to detect when the player touches the sell part.
local sellevent = game.ReplicatedStorage.Sell
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print("sell fired")
sellevent:Fire()
end
end)
That script works. But that is a server script. Let me know if I should change it to a LocalScript. Now, here is my script that changes the leaderstats, it detects the the event fired but nothing changes in leaderstats.
game.ReplicatedStorage.Sell.Event:Connect(function(player)
print("bindable event fired")
if player ~= nil then
if player.leaderstats.Sticks.Value > 0 then
player.leaderstats.Cash.Value += player.leaderstats.Sticks.Value
player.leaderstats.Sticks.Value = 0
end
end
end)
I gave as much information as I could. If you need anything else I can get more pictures or videos but that is the best I could give. Any help appreciated!
Okay, after watching the video, I might have a hunch as to what's going wrong.
The code in your original question works just fine. If you were to print out the Sticks.Value before adding it to Cash.Value, you'd see that Sticks.Value stays at 0. And you were always adding 0 to Cash.
This is likely because you are using a Tool to increment Sticks.Value, and it is common for Tools to use LocalScripts to hook up logic. When you make changes to the world (or leaderstats values) in LocalScripts, those changes are only present on that client. They are not replicated up to the server. And since all of your cash logic is in server scripts, the Sticks value stays at 0 on the server.
So to fix this, you need to make sure that the code that increments Sticks.Value happens in a Script. And you can do that by using RemoteEvents in the same way you're using your BindableEvent, to communicate from the tool up to a server script.
So in your Tool's LocalScript you'd do something like this :
-- find the RemoteEvent saved in ReplicatedStorage
local stickEvent = game.ReplicatedStorage.GetSticksEvent
-- listen for when the Tool is used
script.Parent.Activated:Connect(function()
-- tell the server to give us some sticks...
stickEvent:FireServer()
end)
Then, in a server Script, listen for that RemoteEvent to fire to give the player some sticks :
-- find the RemoteEvent saved in ReplicatedStorage
local stickEvent = game.ReplicatedStorage.GetSticksEvent
-- listen for when the client tells us that they got some sticks
stickEvent.OnServerEvent:Connect(function(player)
-- give the player some sticks
player.leaderstats.Sticks.Value += 1
end)
Edit - in this most recent version, your Sell script has stopped working because the player argument is nil. In an earlier version of this code, the Touched script passed in the player, but you removed that code and broke the Sell script. So just rollback the script to its earlier state :
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.BindableEvents.Sell:Fire(player)
end
end)

How do I get localplayer data from a leaderboard? (Roblox)

I'm new to Lua and Stack so I apologize in advance.
I'm currently using a tycoon kit to create a Roblox game, and the player's money value only shows up on the leaderboard, but nowhere on the screen. This can make it hard to know how much money you have on console and mobile, so I'm trying to set up a GUI to display the player's amount of money on the screen. Now, setting up the GUI isn't the issue, knowing what to set the text value on the GUI is.
I do not know how to upload am image, but the path in my workspace looks a bit like this:
Players (on the first tab of the workspace)-> Player-> leaderstats-> Cash
I've tried to grab the variable (named "Cash") like this: game.Players.Player.leaderstats.Cash.Value but because I'm not named "Player", it didn't work. I've tried replacing it with LocalPlayer (looked like game.Players.LocalPlayer.leaderstats.Cash.Value), but that also didn't work. It only works when I replace "Player" with my username (game.Players.Username.leaderstats.Cash.Value), but I want this to work for other players besides me.
I've also tried to locate the original variable, but because I did not make the kit I'm at a loss. I've looked in every script that comes with it, but could not find anything.
When I try to set the text to these values, nothing happens. The error message I get falls along the lines of Player is not a valid member of Players.
Any help would be greatly appreciated!
LocalPlayer Is a variable which is only on the client side to get the leaderstats of the LocalPlayer You need to use a local script instead of a normal script
Now i will show you how you can display it first create your "ScreenGui" then create a "TextLabel" under the ScreenGui And finaly create a "LocalScript" Under the TextLabel Now the code should be in the LocalScript
game.Players.LocalPlayer.leaderstats.Cash.Changed:Connect(function(NewValue) --Chnage Cash to your stat
script.Parent.Text = "$"..NewValue
end)
at the start of the script put this:
local plr = game:GetService("Players").LocalPlayer
and at the part where it puts the money on the screen:
(assuming the local script is in the textbox the money is in)
while true do
script.Parent.Text = plr.leaderstats.Cash.Value
wait()
end
and that's all you have to do!
raw paste (just copy and paste this into a localscript inside a textbox)
local plr = game:GetService("Players").LocalPlayer
while true do
script.Parent.Text = plr.leaderstats.Cash.Value
wait()
end
Because LocalPlayer is a Client-Side keyword,
Put this LOCALSCRIPT as a child in the TextLabel. The textlabel should be in a frame. the frame should be in a gui.
Game.Players.LocalPlayer.leaderstats.Cash.Changed:Connect(function(updategui)
script.Parent.Text = "$"..Game.Players.LocalPlayer.leaderstats.Cash.Value
end)

Destroyed part is not destroyed globaly in Roblox

What I'm trying to do: I want to have a tree and every few seconds an Apple falls down from that tree. Player can "pick up" that Apple. If more players are in the game, player who picks up the most apples, wins.
What I have: I have a tree and apples are falling down. Until here it works perfect. Player can pick up an apple - if he touches the apple by his foot, apple is destroyed and player gets 1 point. Still OK.
What is wrong: If more players join the game, it looks like every player can see his own (local) apple. So if Player1 picks up an apple, apple is destroyed - but only for him :( all other players can see that apple still there and they can pick it up too. If I test-run the game with 2 players, in the server-window I can see that apple still there, even after all players picked it up. So the server has it's own instance apparently.
But I want just one global apple.
Application is like this:
I have an apple in the Workspace. Every few seconds I clone it in the script (not local script, but Script) which is under the AppleTree model in Workspace:
function GrowNewApple()
local newApplePos = GetRandomPlace()
local appleTemplate = workspace.apples.prototype
local newApple = appleTemplate:Clone()
newApple.Parent = appleTemplate.Parent
newApple.Name = "apple"
newApple.Position = newApplePos
end
In StarterPlayer / StarterPlayerScripts I have a localscript with this:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
character:WaitForChild("LeftFoot")
character.LeftFoot.Touched:Connect( PickUpApple )
And finally my PickUpApple function looks like this:
function PickUpApple( touchObject )
if touchObject:IsDescendantOf(workspace.apples) then
touchObject:Destroy()
end
end
Any ideas please?
Is it because the PickUpApple() is called from LocalScript? Is it possible that this LocalScript is sending local touchObject into this function?
I have no idea how to do this. Thanks guys.
Deleting the apple from a local script will only delete it for the client, To prevent this, Try it so the apple gets deleted by a server side script, You have 2 options:
1, Make the script a server side script and make sure it's compatible for the server.
2, Make a remote event which is fired once the local script detects the local player touching an apple, And make sure the remote event is connected to a function that deletes the apple and gives the player a point, Should be a server script, To do that:
1, Create a RemoteEvent (Make sure it's a RemoteEvent not a RemoteFunction!) in the ReplicatedStorage and rename it to "PickupApple".
2, Change the local script to:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local event = game.ReplicatedStorage:WaitForChild("PickupApple")
local apples = workspace:WaitForChild("apples") -- Using WaitForChild() function to prevent errors for requesting the object before it loads
character:WaitForChild("LeftFoot")
character.LeftFoot.Touched:Connect(function(touchObject)
if touchObject:IsDescendantOf(apples) then
event:FireServer(touchObject)
end
end)
3, Create a script (Not a LocalScript!) in the ServerScriptService, And put this:
game.ReplicatedStorage.PickupApple.OnServerEvent:Connect(function(player, item)
if item:IsDescendantOf(workspace.apples) then
item:Destroy()
-- Add here any extra code such as giving points, etc
end
end)
OK, problem solved.
The problem was that Touched event was fired on Local Player parts (feet, legs). This has sent the local instance of the apple to the Touched Event Handler.
Now I removed this:
character.LeftFoot.Touched:Connect( PickUpApple )
and instead of firing Touched on player foot I moved it to the Apple part and now I'm firing Touched event on that Apple part.
apple.Touched:Connect(PickUpApple)
And it works. While Apple part sends to the Touched Event Handler player's foot which is OK - I don't need to destroy it - I can destroy Apple now.
I have to say I moved whole function PickUpApple() into the apple part also so I have direct access to the apple part itself.

Resources