Im making a PropHunt game in roblox but the whenever i morph the player character into a new form the UI keeps on being called - roblox

I have this project, im creating a prophunt game, however im in a little road block that i can't understand.
Here is how my code works, I have a local script which tells me which team the player is in, since the player does not have a team yet they are always assumed as neutral, then the local script is called whenever the players are added, which a GUI appears, allowing for them to pick the team.
Heres what i did for the Local script GUI, I'm just learning this for now so not really the best way i could have coded it right now, but it does simulate what i want to happen.
Player.PlayerAdded:Connect(function(player)
if player.Team == TeamService.Blue then
Frame.Visible = false
else
Frame.Visible = true
end
end)
Hiders.MouseButton1Click:Connect(function(player)
local result = ReplicatedStorage.RemoteFunction:InvokeServer(TeamHiders)
print(result)
if result == true then
Frame.Visible = false
else
Frame.Visible = false
end
end)
Here's what i did for the server script to assign the players a team, I'm sorry I'm still fairly new to roblox so i don't know if my approach is correct im just going with what closely gets me to the output.
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function (player, Team)
if Team == teamService.Blue then
player.Team = Team
print(ChoosenTeam)
return "player Team is blue"
end
if Team == teamService.Red then
player.Team = Team
print(player.Team)
return "player Team is red"
end
end
And here is my script for the Morph, so i got this click detector which should tell the game that whenever its clicked transform the player into whatever form this is scripted on.
local model = script.Parent.Parent
script.Parent.MouseClick:Connect(function(player)
if player.Team == teamservice.Blue then
local oldCharacter = player.Character
local newCharacter = model:Clone()
newCharacter.HumanoidRootPart.Anchored = false
newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
player.Character = newCharacter
newCharacter.Parent = workspace
end
end)
The players under blue side should only encounter this once since the UI should only appear when they do not have a team or are loaded in the game, but what happens is the blue players whenever they morph into a block they encounter the GUI prompt again.
I was expecting by limiting the player teams to nil it would only show the UI, and if the player has a team already then the UI would not show itself, the same goes to when the player decides to morph the player UI should not be visible.

So turns out i can just turn off spawn on reset.

Related

Roblox - detect if mesh is hit by hand

I am not sure where to start so asking for some help. I want to create a script that detect if a certain mesh is hit by players right hand. Every time the mesh is hit I want to increase points for player by 1.
Anyone who can nudge me in the right direction here?
Thanks!
EDIT:
I have added this script in StarterCharacterScripts:
game.Players.LocalPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
local part1 = workspace.CoinsClouds["Meshes/SackOfGoldNoCoins1"]
part1.Touched:Connect(function(hit)
if hit.Name == "RightHand" then
print(hit.Name)
end
end)
end)
This will register when I bump into the part with the right hand, BUT it will register it 5-20 times in a split second every time I bump into the part with the right hand. See attached image. Anyone know why? I would like it to register only once when the right hand is bumped against the part, or even better, only when the user punch the part/mesh. I have tried to add a wait after RightHand is found, but that doesn't work.
PS! I don't know if this is the right way to script it...
Below is an example of the code you would use. It will check if your part is touched by either the right or left hand and it will run whatever is inside of your if statement, in this case it will increase whatever your score is by 1.
local part = workspace.Part -- reference the part here
local debounce = false --used to check for debounce
part.Touched:Connect(function(hit)
if hit.Name == "RightHand" or "LeftHand" then -- checks if a hand touched it
if not debounce then --Check that debounce variable is not true
debounce = true --currently touching the part
print("hit")
wait(1)
debounce = false
end
end
end)
In relation to your new question it is to do with debounce. Roblox has an entire article on it here: https://developer.roblox.com/en-us/articles/Debounce
Basically you would have to add a new if statement to check that the player isn't already touching this part if it is it wont repeat the what is inside the if statement. The code sample above has been edited to work with debounce.

Can i use an if statement for a player name? [ROBLOX]

I am on Roblox again, and I wanna make a script where when a part is touched, if the player name is lorenzoomh, set the brickcolor to green. But if not, set it to red. my current code:
script.Parent.Touched:Connect(function(igottouched)
if game.Players.LocalPlayer.Name == "lorenzoomh" then
script.Parent.BrickColor = BrickColor.Green()
else
script.Parent.BrickColor = BrickColor.Red()
end
end)
which doesnt work btw
The answer to your question, "can I use a player's name in an if-statement" is yes. Your problem is that the LocalPlayer doesn't exist where you're trying to use it from.
See the docs for Players.LocalPlayer :
This property is only defined for LocalScripts ... as they run on the client. For the server (on which Script objects run their code), this property is nil.
You need to access the Player object a different way. The Touched event on a Part gives you access to the other object that touched it. You can use that to check if the other part belongs to a player's character model.
local target = game.Workspace.Part
-- listen for a player touching a block
target.Touched:Connect( function(otherPart)
-- check that the thing that touched is a character model
local character = otherPart.Parent
local isCharacter = character:FindFirstChild("Humanoid") ~= null
if isCharacter then
-- the character model name is usually the name of the player so use that
if character.Name == "lorenzoomh" then
target.BrickColor = BrickColor.Green()
else
target.BrickColor = BrickColor.Red()
end
end
end)
The Players service has a method for correlating a character model with a Player object specifically if a simple name check is not sufficient for your use case.

How do you teleport characters in Roblox without leaving residue?

How do you teleport characters in Roblox without causing the HumanoidRootPart to be out of sync with the character? I have found that moving just the HumanoidRootPart will cause the rest of the character to snap back in place where you want them to be, but to the server, it is still in place before the teleport. Is there a better method?
Yes, you can use model:MoveTo() to move models (characters are models).
For example, a simple teleporter:
local debounce = false
-- the teleporter is just a part with the script it's child
script.Parent.Touched:Connect(function (hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
local char = player.Character
local rootPart = char:WaitForChild("HumanoidRootPart")
char:MoveTo(rootPart.Position + Vector3.new(0, 10, 0))
wait(.5)
debounce = false
end
end)

Roblox - while testing trusses work when i drag them in, but when i use script to set all the stuff, then i cant climb it

I am using a script to set cancollide = true and transparency = 0, but i cant climb the truss. but when I'm already in testing mode (in studio) and i drag in the same truss, i can climb it, and i am looking at properties, its same, both anchored is true, they are touching same parts, i dont know why this is happening. Please help thanks :)by the way, i am making tycoon and this is script i am using:
wait(1)
amount = 0 -- cost of model
owner = script.Parent.Parent.Owner
local stun = false
pcall(script.Parent.Head.Touched:connect(function(hit)
if hit.Parent ~= nil then
player = game.Players:findFirstChild(hit.Parent.Name)
if not stun and player ~= nil then
if player.Name == owner.Value then
if player:findFirstChild("leaderstats") ~= nil then
stats = player:findFirstChild("leaderstats")
if stats.Money.Value >= amount then
stun = true
stats.Money.Value = stats.Money.Value - amount
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
wait(1)
stun = false
end
end
end
end
end
end))
dont worry about other stuff, it works, its just this part that matters now:
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
Please help :( this is the problem with the ladder using script not working and same dragged in from toolbox truss working, iv done this with many trusses and ladders and same result :(
So, a error I noticed in your code, is that you erase your Character's Head, which would kill your character, or possibly throw a error if the .Touched event fires two times.
There are many efficency errors in this script, such as .Transparency = (0), or doing script.Parent.ladder instead of using variables, but it really is no problem most of the times. Something you could try, is to use Instance.new() to create the ladder.If this game is FilteringEnabled on (Expermiental mode off), please note your script won't work as intended if it is a LocalScript, it will only work well on Server-Side (aka, a normal Script instead of a LocalScript)

Animation Script not working in ReplicatedStorager

I have a a Gun model that shows up on my screen using the code from https://www.youtube.com/watch?v=VDYtZxnz7FI&t=25s youtube video. But when I try to add animations to it the animations dont work. What I think is happening is that the script isnt loading in because its in Replicated Storage. I Know this because I tested a simple print command and it worked when the script was in the workspace but not in replicated storage. I cant move the model to the workspace because the code depends on it being in replicated storage. If anyone could help me that would be much abliged. Thank You! Im not new to the Roblox scene but im just starting as a Scripter, Either then a mesh modeler.[Picture of Explorer][1]
Here Is the code that puts the viewmodel on my screen
local cam = workspace.CurrentCamera
local run = game:GetService("RunService")
local reps = game:GetService("ReplicatedStorage")
local model = reps:WaitForChild("Henry Rifle"):Clone()
for i,v in pairs (model:GetChildren()) do
if v:IsA("BasePart") then
if v ~= model.PrimaryPart then
local weld = Instance.new("Weld")
weld.Part0 = model.PrimaryPart
weld.Part1 = v
weld.C0 = model.PrimaryPart.CFrame:inverse()
weld.C1 = v.CFrame:inverse()
weld.Name = v.Name
weld.Parent = model.PrimaryPart
end
end
end
model.Parent = cam
run.RenderStepped:connect(function()
model:SetPrimaryPartCFrame(cam.CFrame *CFrame.new(0,-1.5,1.5))
end)
Here is the simple code that start the animation when the game starts
(Used for Testing)
local player = game.Players.LocalPlayer
local controller = script.Parent.Humanoid
local inspect = controller:LoadAnimation(script.Parent.Inspect)
inspect.Looped = true
inspect:Play()
I'm pretty sure your script doesn't work because Roblox forces Filtering Enabled now.
Filtering Enabled (FE)
Difference with non FE and FE games
With FE, Changes made by the Client/Roblox Player is sent to the server, but the server will never replicate the changes made by the Client to others Clients in the same game server.
That video was made when FE was still an option. Roblox forces FE now, this is why your script doesn't work.
Only way for Client to replicate with others Clients, You must use Remotes.
Roblox wiki explains better than me FE and Remotes, so check it out: http://roblox.wikia.com/wiki/Replication_filtering